componentWillMount()
componentDidMount()
shouldComponentUpdate()
componentDidUpdate()
componentWillUnmount()
接下来的几节课将讲述这些生命周期方法的一些基本用例。
注意: `componentWillMount` 生命周期方法会在版本 16.X 废弃在版本 17 移除 [(Source)](https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html)
componentWillMount()
方法在render()
方法之前被调用。在componentWillMount()
中将一些内容记录到控制台--你需要打开浏览器控制台以查看输出。
MyComponent
应该渲染一个div
元素。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find('div').length === 1; })());
- text: 应该在componentWillMount
中调用console.log
。
testString: assert((function() { const lifecycle = React.createElement(MyComponent).type.prototype.componentWillMount.toString().replace(/ /g,''); return lifecycle.includes('console.log('); })());
```