Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何将状态设置为props值,并在react中第一次渲染时将该值显示在表单中_Reactjs_Redux - Fatal编程技术网

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 })
  }
}
请为您的问题添加一个解决方案。