I'm using React.js, and as you know, componentWillMount() is going to be deprecated.
I wanna replace my componentWillMount
s.
I'm going to move its logics into constructor
. Is there any difference between executing some logic in componentWillMount
and in constructor
?
For example,
before
class Hello extends React.Component {
componentWillMount() {
doSomething();
}
render() {
return <div>{this.state.name} </div>
}
}
after
class Hello extends React.Component {
constructor(props) {
super(props);
doSomething();
}
render() {
return <div>{this.state.name} </div>
}
}
Also, when doSomething is setState, is there any difference setting state in constructor and in public prop?
in constructor
constructor(props) {
super(props);
this.state = { foo: 1 };
}
in public prop
state = { foo: 1 };
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…