【问题标题】:The Change in value of prop doesn't make the child component re renderprop 值的变化不会使子组件重新渲染
【发布时间】:2019-09-29 03:45:42
【问题描述】:

我有一个父组件,它将数组道具传递给子组件。但是当渲染在 componentDidMount 之前运行时,我将值设置为该数组.. 子组件收到一个空数组作为道具。
我在网上搜索,似乎 componentWillRecieveProps 可以帮助将子组件制作为 rr 渲染.. 但由于此方法在 react 16 及更多版本中已被弃用。 goto 应该怎么做。

【问题讨论】:

  • 您可以使用 getDerivedStateFromProps 代替 componentWillRecieveProps 但它不仅仅是名称更改)。它是一个静态方法,在组件实例化之后或接收新道具之前调用,与 componentWillReceiveProps 不同。

标签: reactjs react-native


【解决方案1】:

只有在 props 值被填充后才调用子组件

this.state.arrayProp.length && <ChildComponent />

现在当您在componentDidMount 中填充arrayProp 数组状态时,将在componentDidMount 之后再次调用render 方法,因此,现在将使用正确填充的数组值调用ChildComponent

希望对你有帮助!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2018-10-14
    • 2020-04-18
    • 2016-08-28
    • 2018-12-31
    • 2021-01-14
    • 2021-07-09
    相关资源
    最近更新 更多