ReactJS 中的 componentWillMount 和 componentDidMount 有什么区别? [英] What is the difference between componentWillMount and componentDidMount in ReactJS?
问题描述
我查看了 Facebook 的文档 (React.Component) 和它提到了如何在客户端/服务器上调用 componentWillMount
而 componentDidMount
仅在客户端上调用.componentWillMount
对服务器做了什么?
I looked at Facebook's documentation at (React.Component) and it mentions how componentWillMount
is invoked on the client/server whereas componentDidMount
is invoked only on the client. What does componentWillMount
do to the server?
推荐答案
componentWillMount 本质上是构造函数.您可以设置不影响渲染的实例属性,从存储中同步提取数据并使用它设置状态,以及设置组件时需要运行的其他简单的无副作用代码.
componentWillMount is essentially the constructor. You can set instance properties that don't affect render, pull data from a store synchronously and setState with it, and other simple side effect free code you need to run when setting up your component.
很少需要它,对于 ES6 类根本不需要.
It's rarely needed, and not at all with ES6 classes.
这篇关于ReactJS 中的 componentWillMount 和 componentDidMount 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!