【问题标题】:react-intl: How can update child component without passing "this.props.intl"react-intl:如何在不传递“this.props.intl”的情况下更新子组件
【发布时间】:2019-01-17 06:46:55
【问题描述】:

当我使用 intl 库从父组件更改语言对象时,我需要将 intl={this.props.intl} 传递给子组件进行更新。我正在使用FormattedMessage id="example"this.props.intl.formatMessage({ id: "example" })。我有一个父子组件链。所以我需要在每个孩子或某些组件中多次使用 intl 作为道具。如果我需要更新子组件,那么我需要在每个子组件中传递 intl。我需要一个解决方案。我不想在每个子组件中传递 this.props.intl。

【问题讨论】:

  • 你使用的是哪个版本的 React?

标签: reactjs react-intl


【解决方案1】:

当您必须通过多个子链传递道具时,请使用上下文。

Here 是 CodeSandbox 示例。

关于何时以及如何使用上下文,请查看this 文档。

【讨论】:

    【解决方案2】:

    考虑使用Context API,当你想共享组件树共有的东西时,你可以使用它。

    也请查看https://hackernoon.com/how-do-i-use-react-context-3eeb879169a2

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2017-01-29
      • 2018-04-13
      • 2018-07-11
      • 2020-09-14
      • 2018-01-31
      • 1970-01-01
      • 2020-08-18
      • 2022-09-27
      相关资源
      最近更新 更多