我是ReactJS的新手,我制作了一个应用程序,您可以在其中提交名称和电子邮件。名称和邮件应显示在页面底部的列表中。它显示一小段时间,但是随后构造函数被调用并清除状态和列表。
为什么在状态更改后调用构造函数?我以为构造函数只运行一次,然后render-method在setState()更改状态后运行。
class App extends React.Component {
constructor(props) {
super(props);
console.log("App constructor");
this.state = {
signedUpPeople: []
};
this.signUp = this.signUp.bind(this);
}
signUp(person) {
this.setState({
signedUpPeople: this.state.signedUpPeople.concat(person)
});
}
render() {
return (
<div>
<SignUpForm signUp={this.signUp} />
<SignedUpList list={this.state.signedUpPeople} />
</div>
);
}
}
class SignUpForm extends React.Component {
constructor(props) {
super(props);
console.log("SignUpForm constructor");
this.state = {
name: "",
email: ""
};
this.changeValue = this.changeValue.bind(this);
this.onSubmitForm = this.onSubmitForm.bind(this);
}
changeValue(event) {
const value …Run Code Online (Sandbox Code Playgroud)