<分区>
我应该使用哪种生命周期方法来设置表单中的现有值?我做不到<input value={this.props.title} />
因为我想将输入作为受控组件。
我可以在异步方法之后做一个 promise ,但这会是反模式吗?
componentDidMount() {
this.props.fetchItem(this.itemId)
.then(v=>{this.setState({title: v.title})})
}
<分区>
我应该使用哪种生命周期方法来设置表单中的现有值?我做不到<input value={this.props.title} />
因为我想将输入作为受控组件。
我可以在异步方法之后做一个 promise ,但这会是反模式吗?
componentDidMount() {
this.props.fetchItem(this.itemId)
.then(v=>{this.setState({title: v.title})})
}
最佳答案
您可以使用 componentWillReceiveProps() 方法将 Prop 值设置为状态。
像这样。
componentWillReceiveProps(nextProps){
// store your props value to state here
if(nextProps.formData !== undefined){
this.setState({ formData: nextProps.formData });
}
}
在输入更改方法上更新状态值
handleChange = (name, value) => {
// update state here
this.setState({ [name]: value });
}
关于javascript - redux 中异步操作后的 setState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48369020/
相关文章:
javascript - 如何在有条件的情况下在另一个组件正下方的无状态组件上呈现函数?
javascript - es6文字字符串连接作为react中的prop
javascript - 无法读取未定义的属性 'currentUser'
javascript - 无法在 React 组件中测试提交处理程序
javascript - react native : Image Not Showing
javascript - 未处理的拒绝(错误): Given action returned undefined React JS