calling ref.current.execute() in functional component fails to produce any results.
See original GitHub issuereact-google-recaptcha version: 2.1.0 react-async-script version: 1.2.0
I have the most simple setup and I cannot seem to get it to work:
import React, { useRef, useState } from "react";
import { ReCAPTCHA } from "react-google-recaptcha";
export default function App() {
const captchaRef = useRef();
const [text, setText] = useState("Start editing to see some magic happen!");
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>{text}</h2>
<button
onClick={(event) => {
setText("yes. it was a click");
captchaRef.current.execute();
}}
>
Click me
</button>
<ReCAPTCHA
ref={captchaRef}
sitekey="6LcYD3EaAAAAAM2GZpkWsEiCPTSMfAzSXiuG4k36"
onChange={(value) => {
setText(`There you go ${value}`);
}}
size="invisible"
/>
</div>
);
}
or check out the codesandbox
The problem is, that the current.execute never does anything. Clicking the button never fires any request. At the same time, a similar example seems to work just fine: https://codesandbox.io/s/gifted-cache-10q74jj593?file=/src/index.js
So what am I missing here? Is’nt my example even more dumbed down and simpler… have I forgot something?
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (1 by maintainers)
Top Results From Across the Web
ref.current is null in gatsby react app when trying to execute ...
You are never populating the reference with any value. Initially is set to null in: const recaptchaRef = React.createRef().
Read more >Hooks - Apollo GraphQL Docs
A callback function that's called when the query encounters one or more errors (unless errorPolicy is ignore ). This function is passed an...
Read more >A Complete Guide to Testing React Hooks - Toptal
This article explores how to test React Hooks and outlines an eight-step testing plan you could employ to test your own projects.
Read more >Understanding common frustrations with React Hooks
React Hooks must be called in a React function component or a custom React Hook function. error. In line 5, const [count, setCount] ......
Read more >Refs and the DOM - React
This is because a new instance of the function is created with each render, so React needs to clear the old ref and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@OdifYltsaeb Thank you for the issue and the codesandbox example, it was incredibly helpful.
I was able to dig into the issue on your codesandbox and after a few minutes of complete bewilderment, I took a step back and started from the top.
Then I noticed the imports.
ReCAPTCHA
needs to be imported as the default.This fixes the issue in that sandbox example because the default export is wrapped in an HOC,
react-async-script
, that loads the google recaptcha javascript package.This issue highlights some improvements we could make to improve the experience moving forward.
Some💡’s:
dev
mode whengrecaptcha
is not loaded and methods are being calledexecute
but google recaptcha script is not loadedI still have this problem, and I’m importing as the default.
Edit: Only for invisible recaptcha. Visible recaptcha works perfectly. Invisible recaptcha, calls to
current.executeAsync()
vanish into the void. No errors, no.then
or.catch
, just silence.current.execute()
likewise does nothing.