What is the purpose of having functions like componentWillMount in React.js?

前端 未结 5 864
一个人的身影
一个人的身影 2021-02-04 00:32

I have been writing components in React.js recently. I have never had to use methods like componentWillMount and componentDidMount.

rend

5条回答
  •  借酒劲吻你
    2021-02-04 01:29

    This is an example of an isomorphic web application that makes use of componentWillMount: https://github.com/coodoo/react-redux-isomorphic-example

    However, I'm 99% certain that it runs the code inside componentWillMount for no reason on the server side (I think using componentDidMount to ensure it was only run client side would have made more sense) as the code which ensures that fetch promises are fulfilled before rendering the page is in server.js not inside the individual components.

    There is discussion on per-component async fetching here: https://github.com/facebook/react/issues/1739 As far as I can tell, there is not a good use case for componentWillMount as far as isomorphism is concerned at least.

提交回复
热议问题