当另一个组件调用loginBefore函数时,我需要将返回的验证码令牌分配给captchaToken变量,但executeRecaptcha始终未定义,因此它执行if语句中的代码块。不知何故,我认为我需要等到executeRecaptcha函数初始化,然后调用getCaptchaToken函数。有什么好的方法可以做到这一点吗?感谢您的帮助。
import { useGoogleReCaptcha } from 'react-google-recaptcha-v3';
export const useAxiosClient = () => {
const navigate = useNavigate();
const { executeRecaptcha } = useGoogleReCaptcha();
const getCaptchaToken = async (action: string) => {
if (!executeRecaptcha) {
console.log('Execute recaptcha not yet available');
return;
}
return await executeRecaptcha(action);
};
const loginBefore = async () => {
const captchaToken = await getCaptchaToken('login');
Run Code Online (Sandbox Code Playgroud)
我尝试在一定的延迟后调用 getCaptchaToken 函数,该函数有效,但并不总是有效,我认为这不是一个好的解决方案。