admin管理员组文章数量:1022956
I want to reload reCaptcha widget on my state change, when I get new current_lang.
I used ponentDidUpdate
ponentDidUpdate() {
if (this.recaptchaInstance) {
this.recaptchaInstance.reset();
}
}
which looks like it re-render ponent, but language stays the same?
this is my ponent
<Recaptcha
ref={e => (this.recaptchaInstance = e)}
sitekey="using_it_just_didnt_copy_it_here"
size="normal"
render="explicit"
hl={language.current_lang}
onloadCallback={this.onloadRecaptcha}
/>
Can someone please point me in right direction? Thanks!
I want to reload reCaptcha widget on my state change, when I get new current_lang.
I used ponentDidUpdate
ponentDidUpdate() {
if (this.recaptchaInstance) {
this.recaptchaInstance.reset();
}
}
which looks like it re-render ponent, but language stays the same?
this is my ponent
<Recaptcha
ref={e => (this.recaptchaInstance = e)}
sitekey="using_it_just_didnt_copy_it_here"
size="normal"
render="explicit"
hl={language.current_lang}
onloadCallback={this.onloadRecaptcha}
/>
Can someone please point me in right direction? Thanks!
Share Improve this question edited Aug 1, 2018 at 14:19 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Aug 1, 2018 at 14:17 Nemanja SrećkovićNemanja Srećković 3595 silver badges18 bronze badges 2-
are you sure
language.current_lang
is actually different when you do the reset? – Chris Commented Aug 1, 2018 at 14:21 - @Chris yes I am, thanks! – Nemanja Srećković Commented Aug 1, 2018 at 14:23
2 Answers
Reset to default 6Perhaps you need to re-mount Recaptcha
rather than just re-rendering it. You should use the key
prop to force a re-mount:
constructor() {
super();
this.key = 0;
}
ponentDidUpdate() {
if (this.recaptchaInstance) {
this.key++;
}
}
<Recaptcha
key={this.key}
ref={e => (this.recaptchaInstance = e)}
sitekey="using_it_just_didnt_copy_it_here"
size="normal"
render="explicit"
hl={language.current_lang}
onloadCallback={this.onloadRecaptcha}
/>
ponentDidUpdate used in this way will make the state of the application inconsistent and/or less predictable. I suggest that hl={language.current_lang} should be hl={state.current_lang}. Updating the state with a new language through setState() will make the view to render again with new values (updated language) and will keep state consistent, predictable and easily debbugable through (I.E.) react dev tools.
I want to reload reCaptcha widget on my state change, when I get new current_lang.
I used ponentDidUpdate
ponentDidUpdate() {
if (this.recaptchaInstance) {
this.recaptchaInstance.reset();
}
}
which looks like it re-render ponent, but language stays the same?
this is my ponent
<Recaptcha
ref={e => (this.recaptchaInstance = e)}
sitekey="using_it_just_didnt_copy_it_here"
size="normal"
render="explicit"
hl={language.current_lang}
onloadCallback={this.onloadRecaptcha}
/>
Can someone please point me in right direction? Thanks!
I want to reload reCaptcha widget on my state change, when I get new current_lang.
I used ponentDidUpdate
ponentDidUpdate() {
if (this.recaptchaInstance) {
this.recaptchaInstance.reset();
}
}
which looks like it re-render ponent, but language stays the same?
this is my ponent
<Recaptcha
ref={e => (this.recaptchaInstance = e)}
sitekey="using_it_just_didnt_copy_it_here"
size="normal"
render="explicit"
hl={language.current_lang}
onloadCallback={this.onloadRecaptcha}
/>
Can someone please point me in right direction? Thanks!
Share Improve this question edited Aug 1, 2018 at 14:19 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Aug 1, 2018 at 14:17 Nemanja SrećkovićNemanja Srećković 3595 silver badges18 bronze badges 2-
are you sure
language.current_lang
is actually different when you do the reset? – Chris Commented Aug 1, 2018 at 14:21 - @Chris yes I am, thanks! – Nemanja Srećković Commented Aug 1, 2018 at 14:23
2 Answers
Reset to default 6Perhaps you need to re-mount Recaptcha
rather than just re-rendering it. You should use the key
prop to force a re-mount:
constructor() {
super();
this.key = 0;
}
ponentDidUpdate() {
if (this.recaptchaInstance) {
this.key++;
}
}
<Recaptcha
key={this.key}
ref={e => (this.recaptchaInstance = e)}
sitekey="using_it_just_didnt_copy_it_here"
size="normal"
render="explicit"
hl={language.current_lang}
onloadCallback={this.onloadRecaptcha}
/>
ponentDidUpdate used in this way will make the state of the application inconsistent and/or less predictable. I suggest that hl={language.current_lang} should be hl={state.current_lang}. Updating the state with a new language through setState() will make the view to render again with new values (updated language) and will keep state consistent, predictable and easily debbugable through (I.E.) react dev tools.
本文标签: javascripthow to force reload of reactrecaptcha on state change (language update)Stack Overflow
版权声明:本文标题:javascript - how to force reload of react-recaptcha on state change (language update) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745545287a2155364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论