Skip to content
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

How to get widget id? #5

Closed
VIEWVIEWVIEW opened this issue Oct 9, 2022 · 3 comments
Closed

How to get widget id? #5

VIEWVIEWVIEW opened this issue Oct 9, 2022 · 3 comments

Comments

@VIEWVIEWVIEW
Copy link

According to the docs the render function will return the widget-id:

If the invocation is successful, the function returns a widgetId (string). If the invocation is unsuccessful, the function returns undefined.

-- https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/

I need this widgetId to call window.turnstile.reset(widgetId) in order to reset the captcha.

@Le0Developer
Copy link
Owner

Would you be fine with the onload callback including the widgetId?

function TurnstileTest() {
    const [widgetId, setWidgetId] = useState(null);
    setEffect(() => {
        if(!widgetId) return;
        // reset widget after 10s
        const timeoutId = setTimeout(() => {
            turnstile.reset(widgetId);
        }, 10000)
       return () => clearTimeout(timeoutId)
    }, [widgetId])
    return <Turnstile
        sitekey="..."
        onVerify={...}
        onLoad={widgetId => setWidgetId(widgetId)}
    />
}

Do note that the onLoad callback can be called multiple times if the widget has to be re-created because an important prop changes (theme, sitekey, action etc).

@VIEWVIEWVIEW
Copy link
Author

Would you be fine with the onload callback including the widgetId?

Yes, that would be fine :)

@Le0Developer
Copy link
Owner

v1.0.4 has been published on NPM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants