Skip to content

Commit 0f25fe3

Browse files
fix(state): isApiReady state update race condition (#421)
1 parent 1991ed1 commit 0f25fe3

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎lib/src/component/HCaptcha.jsx‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import { flushSync } from 'react-dom';
23
import { hCaptchaLoader } from '@hcaptcha/loader';
34

45
import { getFrame, getMountElement } from '../utils.js';
@@ -252,20 +253,21 @@ export class HCaptcha extends React.Component {
252253
}
253254

254255
handleOnLoad () {
255-
this.setState({ isApiReady: true }, () => {
256-
const element = getMountElement(this.props.scriptLocation);
257-
const frame = getFrame(element);
256+
const element = getMountElement(this.props.scriptLocation);
257+
const frame = getFrame(element);
258258

259-
this._hcaptcha = frame.window.hcaptcha;
259+
this._hcaptcha = frame.window.hcaptcha;
260260

261+
flushSync(() => {
262+
this.setState({ isApiReady: true });
263+
});
261264

262-
// render captcha and wait for captcha id
263-
this.renderCaptcha(() => {
264-
// trigger onLoad if it exists
265+
// render captcha and wait for captcha id
266+
this.renderCaptcha(() => {
267+
// trigger onLoad if it exists
265268

266-
const { onLoad } = this.props;
267-
if (onLoad) onLoad();
268-
});
269+
const { onLoad } = this.props;
270+
if (onLoad) onLoad();
269271
});
270272
}
271273

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hcaptcha/react-hcaptcha",
33
"description": "A React library for hCaptcha",
4-
"version": "2.1.0",
4+
"version": "2.2.0",
55
"author": "hCaptcha team and contributors",
66
"license": "MIT",
77
"repository": {

0 commit comments

Comments
 (0)