小白教程
所有教程
关于
Search
172.71.254.81
172.71.254.81
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“React 组件生命周期”的源代码
本页内容
上一节:
React_组件_API
下一节:
React_AJAX
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:React 组件生命周期}}[[Category:React 教程|12]] = React 组件生命周期 = 在本章节中我们将讨论 React 组件的生命周期。 组件的生命周期可分成三个状态: * Mounting(挂载):已插入真实 DOM * Updating(更新):正在被重新渲染 * Unmounting(卸载):已移出真实 DOM == 挂载 == 当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下: * [https://www.xiaobai.wang/react/react-ref-constructor.html <code>constructor()</code>]: 在 React 组件挂载之前,会调用它的构造函数。 * [[react ref getderivedstatefromprops|<code>getDerivedStateFromProps()</code>]]: 在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。 * [[react ref render|<code>render()</code>]]: render() 方法是 class 组件中唯一必须实现的方法。 * [[react ref componentdidmount|<code>componentDidMount()</code>]]: 在组件挂载后(插入 DOM 树中)立即调用。 render() 方法是 class 组件中唯一必须实现的方法,其他方法可以根据自己的需要来实现。 这些方法的详细说明,可以参考[https://zh-hans.reactjs.org/docs/react-component.html#reference 官方文档]。 == 更新 == 每当组件的 state 或 props 发生变化时,组件就会更新。 当组件的 props 或 state 发生变化时会触发更新。组件更新的生命周期调用顺序如下: * [[react ref getderivedstatefromprops|<code>getDerivedStateFromProps()</code>]]: 在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。根据 shouldComponentUpdate() 的返回值,判断 React 组件的输出是否受当前 state 或 props 更改的影响。 * [[react ref shouldcomponentupdate|<code>shouldComponentUpdate()</code>]]:当 props 或 state 发生变化时,shouldComponentUpdate() 会在渲染执行之前被调用。 * [[react ref render|<code>render()</code>]]: render() 方法是 class 组件中唯一必须实现的方法。 * [[react ref getsnapshotbeforeupdate|<code>getSnapshotBeforeUpdate()</code>]]: 在最近一次渲染输出(提交到 DOM 节点)之前调用。 * [[react ref componentdidupdate|<code>componentDidUpdate()</code>]]: 在更新后会被立即调用。 render() 方法是 class 组件中唯一必须实现的方法,其他方法可以根据自己的需要来实现。 这些方法的详细说明,可以参考[https://zh-hans.reactjs.org/docs/react-component.html#reference 官方文档]。 == 卸载 == 当组件从 DOM 中移除时会调用如下方法: * [[react ref componentwillunmount|<code>componentWillUnmount()</code>]]: 在组件卸载及销毁之前直接调用。 这些方法的详细说明,可以参考[https://zh-hans.reactjs.org/docs/react-component.html#reference 官方文档]。 以下是一个当前时间的实例,每秒更新: <sample title="" desc="" lang="html" hererun="1"> class Clock extends React.Component { constructor(props) { super(props); this.state = {date: new Date()}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { return ( <div> <h1>Hello, xiaoBai!</h1> <h2>现在时间是:{this.state.date.toLocaleTimeString()}.</h2> </div> ); } } ReactDOM.render( <Clock />, document.getElementById('root') ); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React 实例</title> <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script> <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="root"></div> <script type="text/babel"> class Clock extends React.Component { constructor(props) { super(props); this.state = {date: new Date()}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { return ( <div> <h1>Hello, xiaoBai!</h1> <h2>现在时间是:{this.state.date.toLocaleTimeString()}.</h2> </div> ); } } ReactDOM.render( <Clock />, document.getElementById('root') ); </script> </body> </html> </run> 以下实例在 Hello 组件加载以后,通过 componentDidMount 方法设置一个定时器,每隔100毫秒重新设置组件的透明度,并重新渲染: == React 实例 == <sample title="" desc="" lang="html" hererun="1"> class Hello extends React.Component { constructor(props) { super(props); this.state = {opacity: 1.0}; } componentDidMount() { this.timer = setInterval(function () { var opacity = this.state.opacity; opacity -= .05; if (opacity < 0.1) { opacity = 1.0; } this.setState({ opacity: opacity }); }.bind(this), 100); } render () { return ( <div style={{opacity: this.state.opacity}}> Hello {this.props.name} </div> ); } } ReactDOM.render( <Hello name="world"/>, document.body ); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React 实例</title> <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script> <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <script type="text/babel"> class Hello extends React.Component { constructor(props) { super(props); this.state = {opacity: 1.0}; } componentDidMount() { this.timer = setInterval(function () { var opacity = this.state.opacity; opacity -= .05; if (opacity < 0.1) { opacity = 1.0; } this.setState({ opacity: opacity }); }.bind(this), 100); } render () { return ( <div style={{opacity: this.state.opacity}}> Hello {this.props.name} </div> ); } } ReactDOM.render( <Hello name="world"/>, document.body ); </script> </body> </html> </run> 以下实例初始化 '''state''' , '''setNewnumber''' 用于更新 '''state'''。所有生命周期在 '''Content''' 组件中。 == React 实例 == <sample title="" desc="" lang="html" hererun="1"> class Button extends React.Component { constructor(props) { super(props); this.state = {data: 0}; this.setNewNumber = this.setNewNumber.bind(this); } setNewNumber() { this.setState({data: this.state.data + 1}) } render() { return ( <div> <button onClick = {this.setNewNumber}>INCREMENT</button> <Content myNumber = {this.state.data}></Content> </div> ); } } class Content extends React.Component { componentWillMount() { console.log('Component WILL MOUNT!') } componentDidMount() { console.log('Component DID MOUNT!') } componentWillReceiveProps(newProps) { console.log('Component WILL RECEIVE PROPS!') } shouldComponentUpdate(newProps, newState) { return true; } componentWillUpdate(nextProps, nextState) { console.log('Component WILL UPDATE!'); } componentDidUpdate(prevProps, prevState) { console.log('Component DID UPDATE!') } componentWillUnmount() { console.log('Component WILL UNMOUNT!') } render() { return ( <div> <h3>{this.props.myNumber}</h3> </div> ); } } ReactDOM.render( <div> <Button /> </div>, document.getElementById('example') ); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React 实例</title> <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script> <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="example"></div> <script type="text/babel"> class Button extends React.Component { constructor(props) { super(props); this.state = {data: 0}; this.setNewNumber = this.setNewNumber.bind(this); } setNewNumber() { this.setState({data: this.state.data + 1}) } render() { return ( <div> <button onClick = {this.setNewNumber}>INCREMENT</button> <Content myNumber = {this.state.data}></Content> </div> ); } } class Content extends React.Component { componentWillMount() { console.log('Component WILL MOUNT!') } componentDidMount() { console.log('Component DID MOUNT!') } componentWillReceiveProps(newProps) { console.log('Component WILL RECEIVE PROPS!') } shouldComponentUpdate(newProps, newState) { return true; } componentWillUpdate(nextProps, nextState) { console.log('Component WILL UPDATE!'); } componentDidUpdate(prevProps, prevState) { console.log('Component DID UPDATE!') } componentWillUnmount() { console.log('Component WILL UNMOUNT!') } render() { return ( <div> <h3>{this.props.myNumber}</h3> </div> ); } } ReactDOM.render( <div> <Button /> </div>, document.getElementById('example') ); </script> </body> </html> </run>
返回至“
React 组件生命周期
”。
上一节:
React_组件_API
下一节:
React_AJAX