Skip to content

Rewrite example using hooks #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions Example.App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import * as React from 'react';
import React, { useState, useRef } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import ConfirmHcaptcha from '@hcaptcha/react-native-hcaptcha';

// demo sitekey
const siteKey = '00000000-0000-0000-0000-000000000000';
const baseUrl = 'https://hcaptcha.com';

export default class App extends React.Component {
state = {
code: null,
};
onMessage = event => {
const App = () => {
const [code, setCode] = useState(null);
const captchaForm = useRef(null);

const onMessage = event => {
if (event && event.nativeEvent.data) {
if (['cancel'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
this.setState({ code: event.nativeEvent.data });
captchaForm.current.hide();
setCode(event.nativeEvent.data);
} else if (['error'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
this.setState({ code: event.nativeEvent.data });
captchaForm.current.hide();
setCode(event.nativeEvent.data);
console.log('Verification failed', event.nativeEvent.data);
} else if (event.nativeEvent.data === 'expired') {
event.reset();
Expand All @@ -26,42 +26,39 @@ export default class App extends React.Component {
console.log('Visual challenge opened');
} else {
console.log('Verified code from hCaptcha', event.nativeEvent.data);
this.captchaForm.hide();
captchaForm.current.hide();
event.markUsed();
this.setState({ code: event.nativeEvent.data });
setCode(event.nativeEvent.data);
}
}
};

render() {
let { code } = this.state;
return (
<View style={styles.container}>
<ConfirmHcaptcha
ref={_ref => (this.captchaForm = _ref)}
siteKey={siteKey}
baseUrl={baseUrl}
languageCode="en"
onMessage={this.onMessage}
/>
<TouchableOpacity
onPress={() => {
this.captchaForm.show();
}}>
<Text style={styles.paragraph}>Click to launch</Text>
</TouchableOpacity>
{code && (
<Text style={{ alignSelf: 'center' }}>
{'passcode or status: '}
<Text style={{ color: 'darkviolet', fontWeight: 'bold', fontSize: 6 }}>
{code}
</Text>
return (
<View style={styles.container}>
<ConfirmHcaptcha
ref={captchaForm}
siteKey={siteKey}
baseUrl={baseUrl}
languageCode="en"
onMessage={onMessage}
/>
<TouchableOpacity
onPress={() => {
captchaForm.current.show();
}}>
<Text style={styles.paragraph}>Click to launch</Text>
</TouchableOpacity>
{code && (
<Text style={{ alignSelf: 'center' }}>
{`passcode or status: `}
<Text style={{ color: 'darkviolet', fontWeight: 'bold', fontSize: 6 }}>
{code}
</Text>
)}
</View>
);
}
}
</Text>
)}
</View>
);
};

const styles = StyleSheet.create({
container: {
Expand All @@ -78,3 +75,4 @@ const styles = StyleSheet.create({
},
});

export default App;
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,33 @@ By default, this value is 120 seconds. So, an `expired` error will be emitted to
Once you've utilized hCaptcha's token, call `markUsed` on the event object in `onMessage`:

```js
onMessage = event => {
const onMessage = event => {
if (event && event.nativeEvent.data) {
if (['cancel'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
captchaForm.current.hide();
} else if (['error'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
captchaForm.current.hide();
// handle error
} else {
this.captchaForm.hide();
captchaForm.current.hide();
const token = event.nativeEvent.data;
// utlize token and call markUsed once you done with it
// utilize token and call markUsed once you are done with it
event.markUsed();
}
}
};
...
<ConfirmHcaptcha
ref={_ref => (this.captchaForm = _ref)}
siteKey={siteKey}
languageCode="en"
onMessage={this.onMessage}
/>

const captchaForm = useRef(null);

return (
<ConfirmHcaptcha
ref={captchaForm}
siteKey={siteKey}
languageCode="en"
onMessage={onMessage}
/>
);
```
```

### Handling errors and retry
Expand Down
Loading