Reactjs 如何将状态设置为props值,并在react中第一次渲染时将该值显示在表单中
如何将状态变量的默认值设置为属性值 我曾尝试在建筑中设置它们:Reactjs 如何将状态设置为props值,并在react中第一次渲染时将该值显示在表单中,reactjs,redux,Reactjs,Redux,如何将状态变量的默认值设置为属性值 我曾尝试在建筑中设置它们: this.state = { a: this.props.a, b: this.props.b } 但当我在表单中使用受控输入时。 这些值将在第一次渲染时设置以前的值,而不是设置的值 使用诸如componentWillMount和componentDidMount之类的生命周期方法会产生相同的结果 我正在考虑使用REF,但还有其他方法吗。在静态定义中尝试使用此方法 this.state = { header
this.state = {
a: this.props.a,
b: this.props.b
}
但当我在表单中使用受控输入时。
这些值将在第一次渲染时设置以前的值,而不是设置的值
使用诸如componentWillMount和componentDidMount之类的生命周期方法会产生相同的结果
我正在考虑使用REF,但还有其他方法吗。在静态定义中尝试使用此方法
this.state = {
header: "Header from state...",
content: "Content from state..."
}
使用以下解决方法
componentWillReceiveProps(nextProps){
if (nextProps.inputValue !== this.props.inputValue) {
this.setState({ inputVal: nextProps.inputValue })
}
}
请为您的问题添加一个解决方案。