gothinkster/react-mobx-realworld-example-app

API calls in componentWillMount vs componentDidMount

Closed this issue · 3 comments

Is there any reason API calls are made in WillMount as opposed to DidMount

If I'm reading this correctly the React docs say use DidMount:

"componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering."
https://facebook.github.io/react/docs/react-component.html

https://daveceddia.com/where-fetch-data-componentwillmount-vs-componentdidmount/

Outstanding example app btw! I appreciate the quality!

Is there any reason API calls are made in WillMount as opposed to DidMount

@avbanks, sorry for the late response, that's just to avoid additional re-rendering while showing spinner after initial rendering, but it doesn't really matter for me. PR is welcome :-)

If @avbanks isn't already working on this, I'd be happy to create a PR for this.