Skip to content

Commit cddcade

Browse files
committed
update readme, update example
1 parent b6262d7 commit cddcade

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

README.md

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status][travis.img]][travis.url] [![npm version][npm.img]][npm.url] [![npm downloads][npm.dl.img]][npm.dl.url] [![Dependencies][deps.img]][deps.url]
44

5-
Component wrapper for [Google reCAPTCHA v2][reCAPTCHA]
5+
React component for [Google reCAPTCHA v2][reCAPTCHA].
66

77
## Installation
88

@@ -12,10 +12,13 @@ npm install --save react-google-recaptcha
1212

1313
## Usage
1414

15-
All you need to do is [sign up for an API key pair][signup]. You will need the client key.
15+
All you need to do is [sign up for an API key pair][signup]. You will need the client key then you can use `<ReCAPTCHA />`.
1616

17-
You can then use the reCAPTCHA. The default require imports a wrapped component that loads the reCAPTCHA script asynchronously.
17+
The default usage imports a wrapped component that loads the google recaptcha script asynchronously then instantiates a `reCAPTCHA` the user can then interact with.
1818

19+
Here is a simple working [example on codesandbox.io](https://codesandbox.io/s/1y4zzjq37l).
20+
21+
Code Example:
1922
```jsx
2023
import ReCAPTCHA from "react-google-recaptcha";
2124

@@ -25,47 +28,31 @@ function onChange(value) {
2528

2629
ReactDOM.render(
2730
<ReCAPTCHA
28-
ref="recaptcha"
2931
sitekey="Your client site key"
3032
onChange={onChange}
3133
/>,
3234
document.body
3335
);
3436
```
3537

36-
### Rendering Props
38+
### Component Props
3739

38-
Other properties can be used to customise the rendering.
40+
Properties used to customise the rendering:
3941

4042
| Name | Type | Description |
4143
|:---- | ---- | ------ |
4244
| sitekey | string | The API client key |
4345
| onChange | func | The function to be called when the user successfully completes the captcha |
44-
| theme | enum | *optional* `light` or `dark` The theme of the widget *(__defaults:__ light)*. See [example][docs_theme]
45-
| type | enum | *optional* `image` or `audio` The type of initial captcha *(__defaults:__ image)*
46-
| tabindex | number | *optional* The tabindex on the element *(__default:__ 0)*
46+
| theme | enum | *optional* `light` or `dark` The theme of the widget *(__defaults:__ `light`)*. See [example][docs_theme]
47+
| type | enum | *optional* `image` or `audio` The type of initial captcha *(__defaults:__ `image`)*
48+
| tabindex | number | *optional* The tabindex on the element *(__default:__ `0`)*
4749
| onExpired | func | *optional* callback when the challenge is expired and has to be redone by user. By default it will call the onChange with null to signify expired callback. |
4850
| onErrored | func | *optional* callback when the challenge errored, most likely due to network issues. |
4951
| stoken | string | *optional* set the stoken parameter, which allows the captcha to be used from different domains, see [reCAPTCHA secure-token] |
5052
| size | enum | *optional* `compact`, `normal` or `invisible`. This allows you to change the size or do an invisible captcha |
5153
| badge | enum | *optional* `bottomright`, `bottomleft` or `inline`. Positions reCAPTCHA badge. *Only for invisible reCAPTCHA* |
5254

53-
54-
__lang__: In order to translate the reCaptcha widget, you should create a global variable configuring the desired language. If you don't provide it, reCaptcha will pick up the user's interface language.
55-
56-
__useRecaptchaNet__: If google.com is blocked, you can set useRecaptchaNet to `true` so that the component uses recaptcha.net instead.
57-
58-
__removeOnUnmount__: If you plan to change the lang dynamically, removeOnUnmount should probably be true. This will allow you to unmount the reCAPTCHA component and remount it with a new language.
59-
60-
```js
61-
window.recaptchaOptions = {
62-
lang: 'fr',
63-
useRecaptchaNet: true,
64-
removeOnUnmount: false,
65-
};
66-
```
67-
68-
## Component API
55+
### Component Instance API
6956

7057
The component instance also has some utility functions that can be called. These can be accessed via `ref`.
7158

@@ -75,23 +62,44 @@ The component instance also has some utility functions that can be called. These
7562
- `execute()` programmatically invoke the challenge
7663
- need to call when using `"invisible"` reCAPTCHA - [example below](#invisible-recaptcha)
7764

65+
Example:
66+
```javascript
67+
const recaptchaRef = React.createRef();
68+
...
69+
onSubmit = () ={
70+
const recaptchaValue = recaptchaRef.getValue();
71+
this.props.onSubmit(recaptchaValue);
72+
}
73+
render() {
74+
return (
75+
<form onSubmit={this.onSubmit} >
76+
<ReCAPTCHA
77+
ref={recaptchaRef}
78+
sitekey="Your client site key"
79+
onChange={onChange}
80+
/>
81+
</form>
82+
)
83+
}
84+
```
85+
7886
### Invisible reCAPTCHA
7987

8088
[Invisible reCAPTCHA](https://developers.google.com/recaptcha/docs/versions)
8189

82-
Starting with 0.7.0, the component now supports invisible options. See the [reCAPTCHA documentation](https://developers.google.com/recaptcha/docs/invisible) to see how to configure it.
90+
See the [reCAPTCHA documentation](https://developers.google.com/recaptcha/docs/invisible) to see how to configure it.
8391

8492
With the invisible option, you need to handle things a bit differently. You will need to call the `execute` method yourself.
8593

8694
```jsx
8795
import ReCAPTCHA from "react-google-recaptcha";
8896

89-
let captcha;
97+
const recaptchaRef = React.createRef();
9098

9199
ReactDOM.render(
92100
<form onSubmit={() => { captcha.execute(); }}>
93101
<ReCAPTCHA
94-
ref={(el) => { captcha = el; }}
102+
ref={recaptchaRef}
95103
size="invisible"
96104
sitekey="Your client site key"
97105
/>
@@ -103,6 +111,25 @@ ReactDOM.render(
103111

104112
### Advanced usage
105113

114+
#### Global properties used by reCaptcha
115+
116+
__lang__: By default google reCaptcha infers the user's interface language. In order to overwrite the language and update the translation for the reCaptcha widget, you can create a global variable configuring the desired language via `lang`.
117+
118+
__useRecaptchaNet__: If google.com is blocked, you can set `useRecaptchaNet` to `true` so that the component uses recaptcha.net instead.
119+
120+
__removeOnUnmount__: If you plan to change the lang dynamically, `removeOnUnmount` should probably be `true`. This unloads the google recaptcha script on `componetWillUnmount` to allow for a new google recaptcha script to load next time the reCAPTCHA component is used to facilitate a new language if needed.
121+
122+
Example global properties:
123+
```js
124+
window.recaptchaOptions = {
125+
lang: 'fr',
126+
useRecaptchaNet: true,
127+
removeOnUnmount: false,
128+
};
129+
```
130+
131+
#### ReCaptcha loading google recaptcha script manually
132+
106133
You can also use the barebone components doing the following. Using that component will oblige you to manage the grecaptcha dep and load the script by yourself.
107134

108135
```jsx
@@ -121,7 +148,7 @@ render(
121148
```
122149

123150
## Notes on Requirements
124-
At least `[email protected]` is required due to `forwardRef` usage in [react-async-script](https://github.com/dozoisch/react-async-script).
151+
At least `[email protected]` is required due to `forwardRef` usage in the dependency [react-async-script](https://github.com/dozoisch/react-async-script).
125152

126153
## Notes
127154

0 commit comments

Comments
 (0)