Answer the question
In order to leave comments, you need to log in
React. Is it necessary to use constructor(){...} to set state based on props?
Found that you can set the state directly, based on props
export default class Task extends Component{
state ={
currentText: this.props.text, // вот так
}
...
constructor(props){
super(props);
this.state = {currentText: this.props.currentText};
}
Answer the question
In order to leave comments, you need to log in
class properties experimental syntax.
In this case, the state field is set after ( simple example ) the call to the React.Component constructor , which initializes the dedicated props . So when you define state , props are already defined and available.
If you use transform-class-properties , it is included in stage-0, stage-1, stage-2, then feel free to use it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question