Closed nmalyarsky closed 2 years ago
"This behavior can be avoided by using a state variable in a consumer code." Do you have an example of this? I've run into the same issue and am a bit confused as to how to stop this frequent reloading.
Sure, using example above:
import type { AppProps } from 'next/app'
function MyApp({ Component, pageProps }: AppProps) {
const [scriptProps] = useState({ async: true })
return (
<GoogleReCaptchaProvider scriptProps={scriptProps} reCaptchaKey="KEY">
<Component {...pageProps} />
</GoogleReCaptchaProvider>
)
}
export default MyApp
@nmalyarsky Thank you!
If the
scriptProps
prop is specified in aGoogleReCaptchaProvider
, then a full cleanup/inject cycle will be initiated on each provider rerender, even ifscriptProps
object stays semantically the same. This is due to the nature ofuseEffect
and object equality.Issue is easily replicated using barebone Next.js app with routing and a custom
App
like this one:Each route change rerenders
GoogleReCaptchaProvider
,{ async: true }
object fails equality check and script reloads.This behavior can be avoided by using a state variable in a consumer code. I'm also ready to open a PR in case you are willing to fix the issue in a library itself.