Skip to content

Commit 6f0af75

Browse files
committed
docs: add an example for the HOC
1 parent ad919ec commit 6f0af75

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

example/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import * as ReactDom from 'react-dom';
33
import { GoogleReCaptchaProvider } from '../src/google-recaptcha-provider';
44
import { GoogleRecaptchaExample } from './google-recaptcha-example';
5+
import { WithGoogleRecaptchaExample } from './with-google-recaptcha-example';
56

67
ReactDom.render(
78
<GoogleReCaptchaProvider
@@ -11,6 +12,7 @@ ReactDom.render(
1112
>
1213
<h2>Google Recaptcha Example</h2>
1314
<GoogleRecaptchaExample />
15+
<WithGoogleRecaptchaExample />
1416
</GoogleReCaptchaProvider>,
1517
document.getElementById('app')
1618
);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React, { Component } from 'react';
2+
import {
3+
IWithGoogleReCaptchaProps,
4+
withGoogleReCaptcha
5+
} from '../src/with-google-recaptcha';
6+
7+
class ReCaptchaComponent extends Component<{}, { token?: string }> {
8+
constructor(props: {}) {
9+
super(props);
10+
11+
this.state = { token: undefined };
12+
}
13+
14+
handleVerifyRecaptcha = async () => {
15+
const { executeRecaptcha } = (this.props as IWithGoogleReCaptchaProps)
16+
.googleReCaptchaProps;
17+
18+
if (!executeRecaptcha) {
19+
console.log('Recaptcha has not been loaded');
20+
21+
return;
22+
}
23+
24+
const token = await executeRecaptcha('homepage');
25+
26+
this.setState({ token });
27+
};
28+
29+
render() {
30+
const { token } = this.state;
31+
return (
32+
<div>
33+
<h3>With Google Recaptcha HOC Example</h3>
34+
<button onClick={this.handleVerifyRecaptcha}>Verify Recaptcha</button>
35+
<p>Token: {token}</p>
36+
</div>
37+
);
38+
}
39+
}
40+
41+
export const WithGoogleRecaptchaExample =
42+
withGoogleReCaptcha(ReCaptchaComponent);

0 commit comments

Comments
 (0)