Skip to content

Commit 4ed3fa5

Browse files
authored
Merge branch 't49tran:master' into inline-badge
2 parents 2c88eb0 + 70e45f6 commit 4ed3fa5

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Same thing applied when you use this library with framework such as Next.js or R
4343

4444
| **Props** | **Type** | **Default** | **Required?** | **Note** |
4545
| --------------- | :------: | ----------: | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
46-
| reCaptchaKey | Boolean | | Yes | Your recaptcha key, get one from [here](https://www.google.com/recaptcha/intro/v3.html) |
46+
| reCaptchaKey | String | | Yes | Your recaptcha key, get one from [here](https://www.google.com/recaptcha/intro/v3.html) |
4747
| scriptProps | Object | | No | You can customize the injected `script` tag with this prop. It allows you to add `async`, `defer`, `nonce` attributes to the script tag. You can also control whether the injected script will be added to the document body or head with `appendTo` attribute. |
4848
| language | String | | No | optional prop to support different languages that is supported by Google Recaptcha. https://developers.google.com/recaptcha/docs/language |
4949
| useRecaptchaNet | Boolean | false | No | The provider also provide the prop `useRecaptchaNet` to load script from `recaptcha.net`: https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally |
@@ -140,7 +140,7 @@ const YourReCaptchaComponent = () => {
140140

141141
const token = await executeRecaptcha('yourAction');
142142
// Do whatever you want with the token
143-
}, []);
143+
}, [executeRecaptcha]);
144144

145145
// You can use useEffect to trigger the verification as soon as the component being loaded
146146
useEffect(() => {

__tests__/google-recaptcha-provider.test.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,44 @@ describe('<GoogleReCaptchaProvider />', () => {
7979
expect(scriptElm!.getAttribute('defer')).toEqual('');
8080
});
8181

82+
it('does not reload script if scriptProps object stays the same', async () => {
83+
const { rerender } = render(
84+
<GoogleReCaptchaProvider reCaptchaKey="TESTKEY" scriptProps={{ nonce: 'NONCE' }}>
85+
<div />
86+
</GoogleReCaptchaProvider>
87+
);
88+
89+
const scriptElm = document.querySelector('#google-recaptcha-v3');
90+
expect(scriptElm).not.toBeNull();
91+
92+
rerender(
93+
<GoogleReCaptchaProvider reCaptchaKey="TESTKEY" scriptProps={{ nonce: 'NONCE' }}>
94+
<div />
95+
</GoogleReCaptchaProvider>
96+
);
97+
98+
expect(scriptElm).toBe(document.querySelector('#google-recaptcha-v3'));
99+
});
100+
101+
it('reloads script on scriptProps changes', async () => {
102+
const { rerender } = render(
103+
<GoogleReCaptchaProvider reCaptchaKey="TESTKEY" scriptProps={{ async: false }}>
104+
<div />
105+
</GoogleReCaptchaProvider>
106+
);
107+
108+
const scriptElm = document.querySelector('#google-recaptcha-v3');
109+
expect(scriptElm).not.toBeNull();
110+
111+
rerender(
112+
<GoogleReCaptchaProvider reCaptchaKey="TESTKEY" scriptProps={{ async: true }}>
113+
<div />
114+
</GoogleReCaptchaProvider>
115+
);
116+
117+
expect(scriptElm).not.toBe(document.querySelector('#google-recaptcha-v3'));
118+
});
119+
82120
describe('when using enterprise version', () => {
83121
it('accept an enterprise prop to load recaptcha from enterprise source', () => {
84122
render(

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-google-recaptcha-v3",
3-
"version": "1.9.6",
3+
"version": "1.9.8",
44
"description": "React component for google-recaptcha v3",
55
"module": "dist/react-google-recaptcha-v3.esm.js",
66
"main": "dist/react-google-recaptcha-v3.cjs.js",
@@ -20,8 +20,8 @@
2020
"url": "https://github.com/t49tran/react-google-recaptcha-v3"
2121
},
2222
"peerDependencies": {
23-
"react": "^17.0",
24-
"react-dom": "^17.0"
23+
"react": "^17.0 || ^18.0",
24+
"react-dom": "^17.0 || ^18.0"
2525
},
2626
"dependencies": {
2727
"hoist-non-react-statics": "^3.3.2"

src/google-recaptcha-provider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export function GoogleReCaptchaProvider({
7575
}>(null);
7676
const clientId = useRef<number | string>(reCaptchaKey);
7777

78+
const scriptPropsJson = JSON.stringify(scriptProps);
79+
7880
useEffect(() => {
7981
if (!reCaptchaKey) {
8082
logWarningMessage(
@@ -138,7 +140,7 @@ export function GoogleReCaptchaProvider({
138140
return () => {
139141
cleanGoogleRecaptcha(scriptId);
140142
};
141-
}, [useEnterprise, useRecaptchaNet, scriptProps, language, reCaptchaKey]);
143+
}, [useEnterprise, useRecaptchaNet, scriptPropsJson, language, reCaptchaKey]);
142144

143145
const executeRecaptcha = useCallback(
144146
async (action?: string) => {

0 commit comments

Comments
 (0)