What is the difference between componentWillMount and componentDidMount in ReactJS?

前端 未结 8 492
Happy的楠姐
Happy的楠姐 2020-11-29 20:05

I looked at Facebook\'s documentation at (React.Component) and it mentions how componentWillMount is invoked on the client/server whereas componentDidMou

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 20:46

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

    There’s a “gotcha,” though: An asynchronous call to fetch data will not return before the render happens. This means the component will render with empty data at least once.

    There is no way to “pause” rendering to wait for data to arrive. You cannot return a promise from componentWillMount or wrangle in a setTimeout somehow.

    https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/birth/premounting_with_componentwillmount.html

    our Component will not have access to the Native UI (DOM, etc.). We also will not have access to the children refs, because they are not created yet. The componentWillMount() is a chance for us to handle configuration, update our state, and in general prepare for the first render. This means we can start performing calculations or processes based on the prop values.

提交回复
热议问题