gpt4 book ai didi

reactjs - 如何确定替换 componentWillReceiveProps 并继续获取 nextProps?

转载 作者:行者123 更新时间:2023-12-02 03:20:00 25 4
gpt4 key购买 nike

我在我的应用程序中运行 React 16.8.4,在某些情况下我确实需要使用 nextProps。正如 React Docs 提到的,componentWillReceiveProps 是一种不安全的方法,不建议再使用它。

我阅读了文档 ( https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key ),但我什至不知道如何使用带有 key 和所有内容的此版本来移植我的旧 componentWillReceiveProps。

我不需要创建一个新的组件实例,我只需要 nextProps!!!我尝试使用 componentDidUpdate(),但我不想获得 prevProps,我想要下一个 PROPS!

你们能帮忙吗?

最佳答案

componentDidUpdate 中的

this.propscomponentWillReceiveProps 中的 nextProps 相同,因此您可以使用它.

// componentWillReceiveProps(nextProps) {
// if (nextProps.someProp !== this.props.someProp) {
// doSomething();
// }
// }

componentDidUpdate(prevProps) {
if (this.props.someProp !== prevProps.someProp) {
doSomething();
}
}

关于reactjs - 如何确定替换 componentWillReceiveProps 并继续获取 nextProps?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55079782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com