Skip to content

Commit 5a65c21

Browse files
authored
Merge pull request #84 from t49tran/doc-update-readme-for-the-hook
docs: update ReadMe.md
2 parents f79ca0b + 529a93f commit 5a65c21

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,37 @@ const MyComponent: FC = () => {
113113

114114
If you prefer a React Hook approach over the old good Higher Order Component, you can choose to use the custom hook `useGoogleReCaptcha` over the HOC `withGoogleReCaptcha`.
115115

116-
It's very simple to use the hook:
116+
The `executeRecaptcha` function returned from the hook can be undefined when the recaptcha script has not been successfully loaded.
117+
You can do a null check to see if it's available or not.
118+
119+
How to use the hook:
117120

118121
```javascript
119122
import {
120123
GoogleReCaptchaProvider,
121124
useGoogleReCaptcha
122125
} from 'react-google-recaptcha-v3';
123126

124-
const YourReCaptchaComponent = () => {
127+
const YourReCaptchaComponent = () => {
125128
const { executeRecaptcha } = useGoogleReCaptcha();
126-
const token = executeRecaptcha("login_page");
127129

128-
return (...)
129-
}
130+
// Create an event handler so you can call the verification on button click event or form submit
131+
const handleReCaptchaVerify = useCallback(async () => {
132+
if (!executeRecaptcha) {
133+
console.log('Execute recaptcha not yet available');
134+
}
135+
136+
const token = await executeRecaptcha('yourAction');
137+
// Do whatever you want with the token
138+
}, []);
139+
140+
// You can use useEffect to trigger the verification as soon as the component being loaded
141+
useEffect(() => {
142+
handleReCaptchaVerify();
143+
}, [handleReCaptchaVerify]);
144+
145+
return <button onClick={handleRecaptchaVerify}>Verify recaptcha</button>;
146+
};
130147

131148
ReactDom.render(
132149
<GoogleReCaptchaProvider reCaptchaKey="[Your recaptcha key]">

0 commit comments

Comments
 (0)