You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey folks, got this working in React 16 and would love to help update your docs, but not sure where to send in the PR. Here's the code I'm using (works in React 17 with TypeScript 4 strict mode).
import{MutableRefObject,forwardRef,memo,useCallback,useEffect,useRef,useState,}from"react";import{PropsWithComponent}from"lib/types";import{WidgetInstance}from"friendly-challenge";interfaceFriendlyCaptchaOptions{
startMode: "auto"|"focus"|"none";}interfaceFriendlyCaptchaProps{
sitekey: string|null|undefined;options?: FriendlyCaptchaOptions;onReady?: (widget: WidgetInstance)=>any;onStarted?: (widget: WidgetInstance)=>any;onPass?: (solution: string)=>any;onFail?: (solution: string|null)=>any;onError?: (error: any)=>any;}exportconstuseFriendlyCaptcha=({
sitekey,
options ={startMode: "none",},
onReady,
onStarted,
onPass,
onFail,
onError,}: FriendlyCaptchaProps,ref: React.Ref<unknown>=null)=>{const[widget,setWidget]=useState<WidgetInstance|null>(null);const[solution,setSolution]=useState<string|null>(null);const[error,setError]=useState<any>(null);constcaptchaRef=useRef<HTMLDivElement>(null);useEffect(()=>{if(ref&&captchaRef){(refasMutableRefObject<unknown>).current=captchaRef.current;}},[captchaRef,ref]);useEffect(()=>{if(captchaRef.current){captchaRef.current.setAttribute("data-sitekey",sitekey||"");captchaRef.current.setAttribute("data-start",options.startMode||"none");constwidget=newWidgetInstance(captchaRef.current,{doneCallback: (solution)=>{// The captcha was completedsetSolution(solution);if(solution?.length>0){onPass?.(solution);}else{onFail?.(solution);}},errorCallback: (error)=>{// The captcha failed to loadsetError(error);onError?.(error);},startedCallback: ()=>{// The captcha was startedonStarted?.(widget);},readyCallback: ()=>{// The captcha is readyonReady?.(widget);},});setWidget(widget);}},[onError,onFail,onPass,onReady,onStarted,options.startMode,sitekey]);conststart=useCallback(()=>{if(widget){widget.start();}},[widget]);constreset=useCallback(()=>{if(widget){widget.reset();}},[widget]);if(!sitekey||!sitekey?.length){console.error("FriendlyCaptcha: Missing siteKey");setError("FriendlyCaptcha: Missing siteKey");}return{
start,
solution,
error,
reset,
captchaRef,};};exportconstFriendlyCaptcha=memo(forwardRef(<Pextends{}={}>(
props: PropsWithComponent<FriendlyCaptchaProps>&{componentProps?: P;},ref?: React.Ref<unknown>)=>{const{component: Component="div", componentProps ={}asP, ...captchaProps}=props;const{ captchaRef }=useFriendlyCaptcha(captchaProps,ref);return<Componentref={captchaRef}{...componentProps}/>;}));FriendlyCaptcha.displayName="FriendlyCaptcha";
Recommend using useMemo if using the component (or someone builds their own), so adding an example like this one (for my React/Next.js build) to the docs would be good:
Hey folks, got this working in React 16 and would love to help update your docs, but not sure where to send in the PR. Here's the code I'm using (works in React 17 with TypeScript 4 strict mode).
Recommend using
useMemo
if using the component (or someone builds their own), so adding an example like this one (for my React/Next.js build) to the docs would be good:The text was updated successfully, but these errors were encountered: