Yes, you read it right! There is no more constructor in class component in react.js. You must have seen the traditional react code similar to below
class App extends Component {
constructor(props) {
super(props);
this.state = {
toggle: true,
};
}
}
as per new react(v0.13.0-beta), this can be written in a new format
class App extends Component {
state = {
toggle: true,
};
}
This was inspired by TypeScriptโs property initializers.
Advantages:
- Much cleaner way to write
Top comments (2)
So, how can we set props value in default state.
class yourComponent extends React.Component {
state = {
x: this.props.initialX,
// You can even call functions and class methods:
y: this.someMethod(this.props.initialY),
};
}