We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

temmietope • 4 months ago

Thanks for this!

Syed Sirajul Islam Anik • 9 months ago
set the initial state in the constructor


I think the constructor is called first then the other cycles. Right?

shashankj • 2 months ago

yes..Actually the author should correct the post because the sequence is
1. constructor
2. ComponentWillMount
3. Render
4. ComponentDidMount
5. Render

Try this code for proof:-
----------------------------

import React, { Component } from 'react';

class App extends Component {
componentWillMount(){
alert('ComponentWillMount');
console.log('First this called');
}
constructor(props){
alert('before super');
super(props);
alert(' before state constructor');
this.state = {
data: 'Jordan Belfort'
}
alert('after state constructor');
}

getData(){
setTimeout(() => {
console.log('Our data is fetched');
this.setState({
data: 'Hello WallStreet'
})
}, 1000)
}

componentDidMount(){
alert('ComponentDidMount');
this.getData();
}

render() {
alert('render');
return(
<div>
{this.state.data}
</div>
)
}
}

export default App;

Risyandi • 1 year ago

Nice, Thanks for the sharing article about this.