【问题标题】:React 16.4 getDerivedStateFromProps and controlled componentsReact 16.4 getDerivedStateFromProps 和受控组件
【发布时间】:2018-06-05 21:50:52
【问题描述】:

最近我重构了我的 Checkbox 组件以使用新的 getDerivedStateFromProps 静态方法,而不是已弃用的 componentWillReceiveProps

现在我遇到了一些问题:

1. 我必须在组件状态中保存另一个 previousValue 并每次将其与来自 state 或 props 的新值进行比较 - (否则它将不起作用,因为调用了 getDerivedStateFromProps每次更新时,状态也会发生变化。

2. getDerivedStateFromProps 每次复选框更改都会调用 4 次,这似乎是一种浪费...

我在这里错过了什么?

code example

【问题讨论】:

  • 通过移除内部状态和 getDetivedStateFromProps 并仅使用父状态来修复。

标签: reactjs react-dom


【解决方案1】:

已通过移除内部状态和 getDetivedStateFromProps 并仅使用父状态来修复。 fixed-code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多