Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class HCaptcha extends React.Component {
}

componentDidMount () { // Once captcha is mounted intialize hCaptcha - hCaptcha
if (this._hcaptcha) {
return;
}
Comment on lines +50 to +52
Copy link
Collaborator

@brdlyptrs brdlyptrs Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this is correct? This check only validates whether the API has loaded within the current window context.

If you continue to follow the logic of this function, we then use this specific later to determine if we should even render hCaptcha or load the API. If the API is loaded, then the render function will be called. From here the render function checks if a captcha already exists by referencing the id of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought if it's safer to use state.captchaId
in general, this logic works as expected, as this._hcaptcha is set only in componentDidMount, so renderCaptcha is still called

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks as the safe solution to me as it's the way to ensure that componentDidMount is called only once
we could actually use any other variable for that
also React.StrictMode calls componentDidMount twice only on dev mode, so it should be safe especially on prod

however, as the alternative, we could go with this PR: #251
please take a look

@brdlyptrs @vladislav-nick-imi @damian-rodriguez-imi


const element = getMountElement(this.props.scriptLocation);
const frame = getFrame(element);
this._hcaptcha = frame.window.hcaptcha || undefined;
Expand Down
Loading