
[英]React 16.4 - manual form input fill along with its updates from getDerivedStateFromProps?
[英]React 16.4 enables getDerivedStateFromProps to be called from state change. How to cope with that?
所以16.4“修复了”getDerivedStateFromProps中的一个错误,现在它在道具更改和状态更改时被解雇了。 显然这是有意的,来自这篇文章: https : //github.com/facebook/react/issues/12898 。 但是对我来说,在州内保存以前的道具是一个重大的过度杀伤,所以我问是否有人在处理这样的案件时做了一个程序:
class Componentche extends React.Component {
state = {
valuesForInput: {
input1: ''
}
}
static getDerivedStateFromProps(props, state) {
if (props.someInitialValuesForInput.input1 !== state.valuesForInput.input1) {
return {
valuesForInput: {
...state,
input1: props.someInitialValuesForInput.input1
}
}
}
return state;
render () {
<Input value='valuesForInput.input1' onChange='(e) => setState({valuesForInput: {...this.state, input1: e.target.value }})'
}
}
所以在上面这种情况下,我永远不会改变输入,因为getDerivedStateFromProps将在接收的新道具和setState触发器上执行,我的条件永远不会是假的。
那么处理这种情况的正确方法是什么? 我是否需要在州内保留旧道具并将其用于条件?
我刚从React看到这篇文章,但他们没有提供可行的替代方案。
谢谢你的帮助!
应对这一问题的主要思想始终是使用一个真理来源。
实际上,他们提供了2个推荐的解决方案,在该博客文章中根本不使用getDerivedStateFromProps
:
以及2种替代方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.