简体   繁体   English

React 组件在 State 更改后并不总是重新渲染

[英]React Component Does not Always Rerender After State Change

I'm working of the internationalization of my app using https://github.com/yankouskia/localize-react .我正在使用https://github.com/yankouskia/localize-react对我的应用程序进行国际化。 I have the following App class.我有以下应用程序 class。

export default class App extends Component {

  constructor(props) {
    super(props)

    this.state = {
      title: "App title",
      language: "EN"
    };

    this.changeLanguage = this.changeLanguage.bind(this)
  }

  changeLanguage(language) {
    this.setState({
      language: language
    }, () => { console.log(this.state) })
  }

  render() {
    return (
      <LocalizationProvider
        disableCache={IS_LOCALIZE_CACHE_DISABLED}
        locale={this.state.language}
        translations={TRANSLATIONS}
      >
        <Router>
          <section>
            <section className="header">
              <Header />
            </section>
            <section className="content">
              <Route exact path="/" component={HomePage} />
              <Language changeLanguage={this.changeLanguage} />
            </section>
            <section className="footer">
              <Footer />
            </section>
          </section>
        </Router>
      </LocalizationProvider>
    );
  }
}

This is the language selector component:这是语言选择器组件:

export default class Language extends Component {

    render() {
        return (
            <div>
                <div className="flag-container">
                    <img onClick={() => this.props.changeLanguage("RO")} src="/static/svg/ro.svg" alt={'romana'} />
                    <img onClick={() => this.props.changeLanguage("EN")} src="/static/svg/gb.svg" alt={'english'} />
                </div>
            </div>
        )
    }
}

Clicking the flag in Language component should translate the text in the App child components.单击 Language 组件中的标志应该翻译 App 子组件中的文本。 And it does.它确实如此。 But there's a catch.但有一个问题。 Scenarios:场景:

  1. Default is EN.默认为 EN。 Click once on RO -> works.单击一次 RO -> 作品。 Click once on EN -> works;点击一次 EN -> 作品;
  2. Default is EN.默认为 EN。 Click once on EN -> nothing happens (normal).单击一次 EN -> 没有任何反应(正常)。 Click once on RO -> nothing changes in the page.在 RO 上单击一次 -> 页面中没有任何变化。 Console log says state.language is RO.控制台日志说 state.language 是 RO。 Click RO once again -> works.再次单击 RO -> 工作。

So if any of the images is clicked twice(or more) in a row, it takes two clicks on the other one to actually view the translated page.因此,如果连续单击任何图像两次(或更多),则需要两次单击另一个图像才能实际查看翻译页面。 Why doesn't it rerender properly from the first click?为什么它不能从第一次点击正确地重新呈现? The state.language is always the proper one before the render method is called.在调用渲染方法之前,state.language 始终是正确的语言。 Console sample:控制台示例:

RO - language before setState
EN - language after setState
app.js:42 render app
header.js:12 render header
app.js:36 {title: "App title", language: "EN"} - console.log in setState() callback

So the issue here is - the translations function below is not returning the new translations to the router.所以这里的问题是 - 下面的翻译 function 没有将新的翻译返回给路由器。 Also the disableCache on your demo website is false which should be set to true .此外,您的演示网站上的 disableCache 为false ,应设置为true

See https://github.com/yankouskia/localize-react/issues/5https://github.com/yankouskia/localize-react/issues/5

 <LocalizationProvider
        disableCache={IS_LOCALIZE_CACHE_DISABLED}
        locale={this.state.language}
        translations={TRANSLATIONS}
      >

I would suggest to write your localization component as a learning experience - It would be similar to your language change function which will return the key from object translations = { en: 1, fr: 2 }我建议将您的本地化组件作为学习经验编写 -这将类似于您的语言更改 function 将返回来自 object 的密钥翻译 = { en: 1, fr: 2 }

Also try to use es6 arrow functions.也尝试使用 es6 箭头函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM