我有一个使用 create-react-app 创建的 React 应用程序。我将 reCaptcha 放在上面并放在下面,一切都很好。令牌回来了,我可以嵌套一个获取并继续我的生活。
componentDidMount(){
if(!this.state.isRecaptchaReady){
window.grecaptcha.ready(function(){
window.grecaptcha.execute('id', {action: 'homepage'})
.then(//...)
});
}
}
除了我不能,因为我知道必须有更好的方法。我想获取令牌并存储它,然后从中调用一个函数,而不是嵌套得太深,我this无法挽救我的生命(我的意思是我可以但不想用一堆绑定)。
requestRecaptchToken = () => {
console.log(window.grecaptcha);
//const tokenPromise = window.grecaptcha.execute('id', {action: 'homepage'});
}
handleRecaptchToken = data => {
const { modal } = this.state;
modal.recaptcha = JSON.parse(data);
this.setState({modal});
}
componentDidMount(){
if(!this.state.isRecaptchaReady){
window.grecaptcha.ready(this.handleRecaptchReady());
}
}
componentDidUpdate(){
if(this.state.isRecaptchaReady){
this.requestRecaptchToken();
}
}
这条线
const tokenPromise = window.grecaptcha.execute('id', {action: 'homepage'});
在外面
window.grecaptcha.ready()
炸毁
错误:引发了跨域错误。React 无法访问开发中的实际错误对象。有关更多信息,请参阅https://reactjs.org/docs/cross-origin-errors.html。
问题是它没有意义,我读了一些 WebPack eval 的东西,但这似乎有点超出我的学习曲线。