Home Web Front-end JS Tutorial What is the difference between vue and react lifecycle? (Compared)

What is the difference between vue and react lifecycle? (Compared)

Sep 19, 2018 pm 04:49 PM
life cycle

This article brings you what is the difference between the vue and react life cycles? (Comparison) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The life cycle of vue is as shown in the figure below (very clear) initialization, compilation, update, destruction

Second example of vue life cycle

Note that after triggering the created event of vue, this will point to the vue instance. This is very important.

nbsp;html>

    
        <meta>
        <title>vue生命周期</title>
        <script></script>
        <meta>
    
    
        <div>
            {{a}}
        </div>
        <div>
            我是内容二
        </div>
        <script>
            var myVue = new Vue({
                el: ".test",
                data: {
                    a: "我是内容,在控制台输入myVue.a=123456,可以改变我的值"
                },
                created: function () { 
                    //在实例创建之后同步调用。此时实例已经结束解析选项,这意味着已建立:数据绑定,计算属性,方法,watcher/事件回调。
                    //但是还没有开始 DOM 编译,$el 还不存在,但是实例存在,即this.a存在,可打印出来 。
                    console.log("建立");
                },
                beforeCompile: function () {
                    console.log("未开始编译");
                },
                compiled: function () { 
                    //在编译结束后调用。此时所有的指令已生效,因而数据的变化将触发 DOM 更新。但是不担保 $el 已插入文档。
                    console.log("编译完成");
                },
                ready: function () { 
                    //在编译结束和 $el 第一次插入文档之后调用,如在第一次 attached 钩子之后调用。注意必须是由 Vue 插入(如 vm.$appendTo() 等方法或指令更新)才触发 ready 钩子。
                    console.log("一切准备好了");
                },
                attached :function () {  //myVue.$appendTo(".test2")暂时触发不了,不知道怎么解决
                    //在 vm.$el 插入 DOM 时调用。必须是由指令或实例方法(如 $appendTo())插入,直接操作 vm.$el 不会 触发这个钩子。
                    console.log("插入DOM成功");
                },
                detached :function () { //触发事件 myVue.$destroy(true),其中参数true控制是否删除DOM节点或者myVue.$remove()
                    //在 vm.$el 从 DOM 中删除时调用。必须是由指令或实例方法删除,直接操作 vm.$el 不会 触发这个钩子。
                    console.log("删除DOM成功");
                },
                beforeDestroy: function () {  //触发方式,在console里面打myVue.$destroy();
                    //在开始销毁实例时调用。此时实例仍然有功能。
                    console.log("销毁前");
                },
                destroyed: function () {   //触发方式,在console里面打myVue.$destroy();其中myVue.$destroy(true)是删除DOM节点,会触发detached函数,但是实例仍然存在
                    //在实例被销毁之后调用。此时所有的绑定和实例的指令已经解绑,注意是解绑不是销毁,所有的子实例也已经被销毁。
                    console.log("已销毁");
                }
        });
        </script>
    
Copy after login

1. React life cycle

The React life cycle is divided into three states 1. Initialization 2 .Update 3. DestructionWhat is the difference between vue and react lifecycle? (Compared)

  • Initialization

1, getDefaultProps()

To set the default props, you can also use ufaultProps to set the default properties of the component.

2. getInitialState()

There is no such hook function when using the es6 class syntax. You can directly use it in the constructor define this.state in. At this time, you can access this.props

3. componentWillMount()

is only called when the component is initialized. It will not be called when the component is updated. It is only called once in the entire life cycle. At this time, the state can be modified.

4. render()

The most important step of react, creating virtual dom, performing diff algorithm, and updating dom tree are all done here. At this point, the state cannot be changed.

5. componentDidMount()

Called after the component is rendered, only called once.

  • Update

6. componentWillReceiveProps(nextProps)

is not called when the component is initialized. Called when new props are accepted.

7. shouldComponentUpdate(nextProps, nextState)

A very important part of react performance optimization. Called when the component accepts new state or props. We can set whether the two props and state before and after comparison are the same. If they are the same, return false to prevent the update, because the same attribute state will definitely generate the same DOM tree, so there is no need Create a new DOM tree and compare the old DOM tree with the diff algorithm to save a lot of performance, especially when the DOM structure is complex

8, componentWillUpdata(nextProps, nextState)

When the component is initialized Not called, only called when the component is about to be updated. At this time, you can modify state

9, render()

Component rendering

10, componentDidUpdate()

It is not called when the component is initialized. It is called after the component update is completed. At this time, the dom node can be obtained.

  • Uninstall

11. componentWillUnmount()

Called when the component is about to be unmounted, some events Listeners and timers need to be cleared at this time.

The above is the detailed content of What is the difference between vue and react lifecycle? (Compared). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

vue3 changed several life cycle functions vue3 changed several life cycle functions Jan 13, 2023 pm 05:57 PM

vue3 changed 4 life cycle functions. The Vue3 combined API cancels the beforeCreated and created hook functions and uses the step hook instead, and this cannot be used in it. The hook functions for component destruction in Vue3 have been changed from destroyed and beforeDestroy to beforeUnmount and unmounted.

How to deal with destruction and life cycle management of C++ function pointers? How to deal with destruction and life cycle management of C++ function pointers? Apr 17, 2024 pm 05:48 PM

In C++, function pointers require proper destruction and life cycle management. This can be achieved by manually destructing the function pointer and releasing the memory. Use smart pointers, such as std::unique_ptr or std::shared_ptr, to automatically manage the life cycle of function pointers. Bind the function pointer to the object, and the object life cycle manages the destruction of the function pointer. In GUI programming, using smart pointers or binding to objects ensures that callback functions are destructed at the appropriate time, avoiding memory leaks and inconsistencies.

The servlet life cycle is divided into several stages The servlet life cycle is divided into several stages Feb 23, 2023 pm 01:46 PM

The Servlet life cycle refers to the entire process from creation to destruction of a servlet, which can be divided into three stages: 1. Initialization stage, calling the init() method to initialize the Servlet; 2. Running stage (processing requests), the container will Request to create a ServletRequest object representing an HTTP request and a ServletResponse object representing an HTTP response, and then pass them as parameters to the service() method of the Servlet; 3. Destruction phase.

Lifecycle functions in Vue3: Quickly master the lifecycle of Vue3 Lifecycle functions in Vue3: Quickly master the lifecycle of Vue3 Jun 18, 2023 am 08:20 AM

Vue3 is currently one of the most popular frameworks in the front-end world, and the life cycle function of Vue3 is a very important part of Vue3. Vue3's life cycle function allows us to trigger specific events at specific times, enhancing the high degree of controllability of components. This article will explore and explain in detail the basic concepts of Vue3's life cycle functions, the roles and usage of each life cycle function, and implementation cases, to help readers quickly master Vue3's life cycle functions. 1. Vue3’s life cycle function

What are the life cycles of vue3 What are the life cycles of vue3 Feb 01, 2024 pm 04:33 PM

vue3的生命周期:1、beforeCreate;2、created;3、beforeMount;4、mounted;5、beforeUpdate;6、updated;7、beforeDestroy;8、destroyed;9、activated;10、deactivated;11、errorCaptured;12、getDerivedStateFromProps等等

How to control the life cycle of Golang coroutines? How to control the life cycle of Golang coroutines? May 31, 2024 pm 06:05 PM

Controlling the life cycle of a Go coroutine can be done in the following ways: Create a coroutine: Use the go keyword to start a new task. Terminate coroutines: wait for all coroutines to complete, use sync.WaitGroup. Use channel closing signals. Use context context.Context.

Variable scope and life cycle in Go language Variable scope and life cycle in Go language Jun 01, 2023 pm 12:31 PM

Go language is an open source statically typed language. It has the characteristics of simplicity, efficiency and reliability, and is increasingly loved by developers. In the Go language, variables are the most basic form of data storage in programs. The scope and life cycle of variables are very important to the correctness and efficiency of the program. The scope of a variable refers to the visibility and accessibility of the variable, that is, where the variable can be accessed. In the Go language, the scope of variables is divided into global variables and local variables. Global variables are variables defined outside a function and can be used anywhere in the entire program

Explain the life cycle of variables in C language Explain the life cycle of variables in C language Sep 02, 2023 pm 07:37 PM

Storage classes specify the scope, lifetime, and binding of variables. To fully define a variable, it is necessary to mention not only its "type" but also its storage class. A variable name identifies a physical location in computer memory where a set of bits is allocated to store the variable's value. Storage class tells us the following factors - Where are the variables stored (in memory or CPU registers)? If not initialized, what is the initial value of the variable? What is the scope of a variable (the scope within which the variable can be accessed)? What is the life cycle of a variable? The lifetime of a lifetime variable defines the duration for which the computer allocates memory (the duration between memory allocation and deallocation). In C language, variables can have automatic, static or dynamic life cycle. Automatic - create a life cycle with automatic

See all articles