Reactjs 我是如何复制状态的
在构造函数中,声明状态s3,它将等于状态s1乘以s2。在页面上显示它Reactjs 我是如何复制状态的,reactjs,Reactjs,在构造函数中,声明状态s3,它将等于状态s1乘以s2。在页面上显示它 static getDerivedStateFromProps(props, state) { return { s1: props.a1 }, { s2: props.a2 }, { s3: props(s2 * s1) } } 如果您有react组件的构造函数,正如您在文章中所建议的那样 constructor(props){ super(props); this.state = { s1:
static getDerivedStateFromProps(props, state) {
return { s1: props.a1 }, { s2: props.a2 }, { s3: props(s2 * s1) }
}
如果您有react组件的构造函数,正如您在文章中所建议的那样
constructor(props){
super(props);
this.state = {
s1:props.a1,
s2:props.a2,
s3:0 //<---------let's initialize with 0
};
this.updateS3 = this.updateS3.bind(this);
}
您必须在模板中的某个位置具有以下行:
<div>
{ this.state.s3 }
</div>
{this.state.s3}
问题是什么?getDerivedStateFromProps
使用此方法的问题是什么?另外,s1、s2
不是变量名,并且返回值看起来无效。请使您的问题具有描述性。你的话题本身并不清楚。
<div>
{ this.state.s3 }
</div>