Answer the question
In order to leave comments, you need to log in
Where to initialize the state of a React component?
So, what's the difference between these options?
class C extends ReactComponent{
constructor(props){
super(props);
this.state = {
isAnimal: true
}
}
render(......)
}
class C extends ReactComponent{
state = {
isAnimal: true
}
render(........)
}
Answer the question
In order to leave comments, you need to log in
In the first one, you have access to `props` and `context` (constructor arguments).
The second, yet unstandardized "sugar" to the first, but without `props` and `context`.
The second option won't work without babel-plugin-transform-class-properties .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question