question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't access state when use it with hooks component

See original GitHub issue
const [email, setEmail] = useState('')

const onVerify = response => {
  console.log(email) ??
}

Email does not update state to the latest. when I set email state to ‘xxx’ (setEmail(‘xxx’))

but outside onVerify function can access email state console.log(email) // ‘xxx’

Can I use reapctcha with hook component?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
stathis-alexandercommented, Jun 10, 2020

Yes, it’s because it’s not rerendering the recaptcha component when the email updates. The way I got around this was to use a state variable for the captcha and then a useeffect which triggers on the captcha state change.

const [captcha, setCaptcha] = useState('');
const [email, setEmail] = useState('');
const recaptchaRef = createRef();

useEffect(() => {
     if (captcha !== '') {
          console.log(captcha, email);
     }
}, [captcha]);

return (
<Reaptcha
          ref={recaptchaRef}
          onVerify={(res) => { setCaptcha(res); }}
          sitekey={siteKey}
          size="invisible"
          onExpire={() => recaptchaRef.current.reset()}
          onError={() => recaptchaRef.current.reset()}
        />
);

You’ll also need a button of some type to call the recaptchaRef.current.execute();.

1reaction
tom-and-jarycommented, Sep 12, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hook: Can't access states in the function - Stack Overflow
I want to use loading and data in the TrackItem component. – Wong. Nov 30, 2019 at 10:14. yeah then you ...
Read more >
Accessing React State in Event Listeners with useState and ...
If you're using React hooks in a component with an event listener, your event listener callback cannot access the latest state.
Read more >
How To Fix The React Hook 'useState' cannot be called in ...
Simply swap out useState with setState; Refactor your component into a function component if you specifically want to use useState. Both options ...
Read more >
How To Manage State with Hooks on React Components
In this tutorial, you'll manage state on functional components using a method ... This tutorial will use hooks-tutorial as the project name.
Read more >
Accessing previous props or state with React Hooks
Leverage the useRef, useState, usePrevious, and useEffect React Hooks to access previous props and states from within functional components.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found