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.

calling ref.current.execute() in functional component fails to produce any results.

See original GitHub issue

react-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:open
  • Created 3 years ago
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
hartziscommented, Apr 8, 2021

@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.

- import { ReCAPTCHA } from "react-google-recaptcha";
+ import ReCAPTCHA from "react-google-recaptcha";

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:
  • Add warnings/logs in dev mode when grecaptcha is not loaded and methods are being called
    • ie: manually trigger execute but google recaptcha script is not loaded
  • Curious if we can get @dozoisch thoughts on removing the default export completely and only have named exports for v3?
6reactions
q00ucommented, May 26, 2021

I 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.

Read more comments on GitHub >

github_iconTop 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 >

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