@redwoodjs/sdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
24 lines (23 loc) • 732 B
JavaScript
"use client";
import { useEffect, useRef, useState } from "react";
export const useTurnstile = ({ siteKey, onSuccess, }) => {
const ref = useRef(null);
const [token, setToken] = useState(null);
useEffect(() => {
if (!ref.current || !window.turnstile)
return;
const widgetId = window.turnstile.render(ref.current, {
sitekey: siteKey,
callback: (newToken) => {
setToken(newToken);
onSuccess?.(newToken);
},
});
return () => {
if (window.turnstile && widgetId) {
window.turnstile.remove(widgetId);
}
};
}, [siteKey, onSuccess]);
return { ref, token };
};