首页 > 其他分享 >react生命周期

react生命周期

时间:2023-02-04 11:33:32浏览次数:36  
标签:count --- 生命周期 console log render react props

总结-旧生命周期

初始化阶段: 由ReactDOM.render()触发---初次渲染

  1. constructor()
  2. componentWillMount()
  3. render()
  4. componentDidMount() ===> 常用
    一般在这个钩子中做一些初始化的事,例如:开启定时器,发送网络请求,订阅消息

更新阶段: 由组件内部this.setState()或父组件render触发

  1. shouldComponentUpdate()
  2. componentWillUpdate()
  3. render() ===> 常用
  4. componentDidUpdate()

卸载组件: ReactDOM.unmountComponentAtNode()触发

组件挂载流程

  1. componentWillUnmount() ===> 常用
    一般在这个钩子中做一些收尾的事,例如:关闭定时器,取消订阅消息
    class Count extends React.Component {
            // 最先调用
            constructor(props) {
                super(props);
                console.log('count---constructor');
                this.state = { count: 0 }
            }

            // 组件将要挂载
            //WARNING! To be deprecated in React v17. Use componentDidMount instead.
            componentWillMount() {
                console.log('count---componentWillMount');
            }

            // 组件挂载完毕的钩子
            componentDidMount() {
                console.log('count---componentDidMount')
            }

            componentWillUnmount() {
                console.log('count---componentWillUnmount');
            }

            // 控制更新阀门
            shouldComponentUpdate(nextProps, nextState) {
                console.log('count---shouldComponentUpdate');
                return true;
            }
            // 组件将要更新的钩子
            //WARNING! To be deprecated in React v17. Use componentDidUpdate instead.
            componentWillUpdate(nextProps, nextState) {
                console.log('count---componentWillUpdate')
            }
            // 组件更新之后
            componentDidUpdate(prevProps, prevState) {
                console.log('count---componentDidUpdate');
            }

            add = () => {
                let { count } = this.state;
                count += 1;
                this.setState({ count: count });
            }
            unload = () => {
                root.unmount(document.getElementById('test'));
            }

            // 初始化渲染,状态更新之后 执行
            render() {
                const { count } = this.state;
                console.log('count---render');
                return (
                    <div>
                        <h2>当前求和为: {count}</h2>
                        <button onClick={this.add}>点我加一</button>
                        <button onClick={this.unload}>卸载组件</button>
                    </div>
                )
            }
        }

父组件render流程

      class A extends React.Component {
            constructor(props) {
                super(props);
                this.state = { carName: '奔驰' };
            }
            changeCar = () => {
                this.setState({ carName: '宝马' });
            }
            render() {
                return (
                    <div>
                        <h1>A</h1>
                        <button onClick={this.changeCar}>换车</button>
                        <B carName={this.state.carName} />
                    </div>
                )
            }
        }

        class B extends React.Component {
            // 组件将要接收到新的props的钩子
            //WARNING! To be deprecated in React v17.
            //  Use new lifecycle static getDerivedStateFromProps instead.
            componentWillReceiveProps(props) {
                console.log('count---componentWillReceiveProps', props)
            }
            render() {
                return (
                    <div>
                        <h1>B</h1>
                        <h2>现在的车: {this.props.carName}</h2>
                    </div>
                )
            }
        }

新生命周期

class Count extends React.Component {

            // 最先调用
            constructor(props) {
                super(props);
                console.log('count---constructor');
                this.state = { count: 0 }
            }

            // 使用场景极其罕见,state值取决于props
            static getDerivedStateFromProps(props, state) {
                console.log('getDerivedStateFromProps', props, state);
                return null;
            }

            // 在最近一次渲染之前调用,传值给DidUpdate
            getSnapshotBeforeUpdate = (prevProps, prevState) => {
                console.log('getSnapshotBeforeUpdate');
                return 'nihao';
            }

            // 组件挂载完毕的钩子
            componentDidMount() {
                console.log('count---componentDidMount')
            }

            componentWillUnmount() {
                console.log('count---componentWillUnmount');
            }

            // 控制更新阀门
            shouldComponentUpdate(nextProps, nextState) {
                console.log('count---shouldComponentUpdate');
                return true;
            }
            // 组件更新之后
            componentDidUpdate(prevProps, prevState, snapshotValue) {
                console.log('count---componentDidUpdate', snapshotValue);
            }

            add = () => {
                let { count } = this.state;
                count += 1;
                this.setState({ count: count });
            }
            unload = () => {
                root.unmount(document.getElementById('test'));
            }

            // 初始化渲染,状态更新之后 执行
            render() {
                const { count } = this.state;
                console.log('count---render');
                return (
                    <div>
                        <h2>当前求和为: {count}</h2>
                        <button onClick={this.add}>点我加一</button>
                        <button onClick={this.unload}>卸载组件</button>
                    </div>
                )
            }
        }

标签:count,---,生命周期,console,log,render,react,props
From: https://www.cnblogs.com/ucbb/p/17091149.html

相关文章

  • 2022React学习笔记,欢迎批评和指正。
    前言:这是一篇自学笔记,帮助自己的React学习,此学习笔记中只记录教程中对我来说比较又触动的点。观看的视频教程链接如下:001_尚硅谷react教程_react简介_哔哩哔哩_bilibili......
  • react是怎样驱动视图刷新的
    默认情况下,当组件中的props和state发生改变,会导致组件重新渲染。父组件的重新渲染会导致子组件的重新渲染。可以使用usememo去进行一些优化,有些在父组件中跟子组件完全无......
  • 686~687 Servlet执行原理 AND Servlet生命周期方法
    Servlet执行原理1.当服务器接受到客户端的请求后,会解析请求URL路径,获取访问的Servlet的资源路径2.查找web.xml文件,是否有对应的<url-pattern>标签体内容3.......
  • #yyds干货盘点#react组件深度解读
        在React中,我们使用组件(有状态、可组合、可重用)来描述UI。在任何编程语言中,你都可以将组件视为简单的函数。    React组件也一样,它的输入是props,输......
  • React-Router6:从入门到实战最佳指南
    前言大家好,我是CoderBin。前段时间学了react-router5后才知道出了6,经典白学......
  • Spring中Bean的生命周期
     作为java开发程序员在面试的时候通常都会被问到Spring完整的生命周期,但是大多数的开发者都回答的不够完整,其实在BeanFactory这个类中Spring源码的作者已经很好的告诉......
  • 如何写出 react 功能
    1.react事件驱动到执行renderer渲染的基本流程   我个人写了2个月的react总结了一下,react相比于vue好像不是面向对象的框架一样,它更像是一个通过固定的......
  • react 中使用redux的createStore 创建公用变量
    import{createStore}from'redux'exportconststore=createStore(addTodo,0);functionaddTodo(state=0,{type,num=1}){switch(type){......
  • Java多线程并发02——线程的生命周期与常用方法
    线程生命周期一个线程不是被创建了马上就开始执行,也不是一直处于执行状态。在线程的整个生命周期中会经历新建(New)、就绪(Runnable)、运行(Running)、阻塞(Blocked)和销毁(Terminate......
  • React Key的作用
    一、key的作用:1.主要是对DOM渲染的性能优化,用来减少没必要的diff算法对比。当列表顺序发生改变时,如果不加key,不管数据是否发生改变,所有列表元素都会重新渲染,当列表......