javascript - how to force reload of react-recaptcha on state change (language update) - Stack Overflow

I want to reload reCaptcha widget on my state change, when I get new current_lang.I used ponentDidUpdat

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
Add a ment  | 

2 Answers 2

Reset to default 6

Perhaps 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745545288a4632309.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信