欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React的生命周期函數(shù)初始掛載更新移除詳解

 更新時(shí)間:2022年08月31日 17:15:27   作者:一劍天門(mén)  
這篇文章主要為大家介紹了React的生命周期函數(shù)初始掛載更新移除詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

概述

在React中,生命周期函數(shù)指的是組件在某一個(gè)時(shí)刻會(huì)自動(dòng)執(zhí)行的函數(shù)

constructor

 在類(lèi)或組件創(chuàng)建的時(shí)候被自動(dòng)執(zhí)行,我們可以說(shuō)它是生命周期函數(shù),但它并不是React所特有的,所有的Es6對(duì)象都有這個(gè)函數(shù),所以并不能說(shuō)它是React的生命周期函數(shù)

初始

當(dāng)數(shù)據(jù)發(fā)生變化時(shí),render函數(shù)會(huì)被自動(dòng)執(zhí)行,符合我們對(duì)React生命周期函數(shù)的定義,所以它是React的生命周期函數(shù),但在初始階段,并不會(huì)有任何的React生命周期函數(shù)被執(zhí)行,但會(huì)執(zhí)行constructor構(gòu)造函數(shù),進(jìn)行組件數(shù)據(jù)的初始化、

import React,{Component} from 'react';
class Demo extends Component{
    constructor(props){
        console.log("初始化數(shù)據(jù)...");
        super(props);
        this.state = {};
    }
    render(){
        return (
            <div>Hello World</div>
        );
    }
}
export default Demo

掛載

頁(yè)面掛載階段,UNSAFE_componentWillMount 頁(yè)面即將render掛載在html前執(zhí)行,以前叫做componentWillMount但React團(tuán)隊(duì)認(rèn)為這些生命周期函數(shù)經(jīng)常被誤解和巧妙的濫用,會(huì)帶來(lái)潛在的問(wèn)題,所以為他們加上了UNSAFE_前綴,當(dāng)然這里的不安全不是指安全性,而是表示使用這些周期函數(shù)在未來(lái)的React版本中更有可能出現(xiàn)錯(cuò)誤。

即將掛載的函數(shù)執(zhí)行完畢,會(huì)進(jìn)行渲染掛載render,之后會(huì)執(zhí)行componentDidMount函數(shù),我們可以把完成掛載后的邏輯寫(xiě)在這個(gè)函數(shù)上。記住,只有組件第一次渲染頁(yè)面才會(huì)執(zhí)行mount

import React,{Component} from 'react';
class Demo extends Component{
    constructor(props){
        console.log("初始化數(shù)據(jù)...");
        super(props);
        this.state = {};
    }
    UNSAFE_componentWillMount(){
        console.log('UNSAFE_componentWillMount');
    }
    render(){
        console.log('render');
        return (
            <div>Hello World</div>
        );
    }
    componentDidMount(){
        console.log('componentDidMount');
    }
}
export default Demo

 更新

數(shù)據(jù)更新階段,state或props發(fā)生變化,頁(yè)面會(huì)重新渲染。

state會(huì)在更新前先執(zhí)行shouldComponentUpdate生命周期函數(shù),這個(gè)函數(shù)比較特殊,它需要有一個(gè)返回值,true或者false,控制頁(yè)面是否需要重新重新渲染,如果僅僅是數(shù)據(jù)發(fā)生變化,我們可以返回false,那么之后的生命周期函數(shù)都不會(huì)執(zhí)行,這樣可以有效的提升我們組件更新的效率。

返回true后,會(huì)執(zhí)行UNSAFE_componentWillUpdate函數(shù)做更新前的準(zhǔn)備,在執(zhí)行render進(jìn)行頁(yè)面的重新渲染,渲染完畢后執(zhí)行componentDidUpdate函數(shù)

import React,{Component} from 'react';
class Demo extends Component{
    constructor(props){
        console.log("初始化數(shù)據(jù)...");
        super(props);
        this.handleClickTest = this.handleClickTest.bind(this);
        this.state = {
            number:1
        };
    }
    handleClickTest(){
        const number = this.state.number + 1;
        this.setState({
            number
        });
    }
    UNSAFE_componentWillMount(){
        console.log('UNSAFE_componentWillMount');
    }
    render(){
        console.log('render');
        return (
            <div onClick={this.handleClickTest}>Hello World</div>
        );
    }
    componentDidMount(){
        console.log('componentDidMount');
    }
    //更新前執(zhí)行
    shouldComponentUpdate(){
        console.log('shouldComponentUpdate');
        return true;
    }
    UNSAFE_componentWillUpdate(){
        console.log('componentWillUpdate');
    }
    componentDidUpdate(){
        console.log('componentDidUpdate')
    }
}
export default Demo

 componentWillReceiveProps生命周期函數(shù),只有一個(gè)組件接收props或者說(shuō)當(dāng)一個(gè)組件是子組件接收props的時(shí)候,它才會(huì)被執(zhí)行,所以我們需要定義一個(gè)子組件接收父組件傳值

import React,{Component,Fragment} from 'react';
import Demo2 from './Demo2';
class Demo extends Component{
    constructor(props){
        console.log("初始化數(shù)據(jù)...");
        super(props);
        this.handleClickTest = this.handleClickTest.bind(this);
        this.state = {
            number:1
        };
    }
    handleClickTest(){
        const number = this.state.number + 1;
        this.setState({
            number
        });
    }
    UNSAFE_componentWillMount(){
        console.log('UNSAFE_componentWillMount');
    }
    render(){
        console.log('render');
        return (
            <Fragment>
                <div onClick={this.handleClickTest}>Hello World</div>
                <Demo2 number={this.state.number}/>
            </Fragment>
        );
    }
    componentDidMount(){
        console.log('componentDidMount');
    }
    //更新前執(zhí)行
    shouldComponentUpdate(){
        console.log('shouldComponentUpdate');
        return true;
    }
    UNSAFE_componentWillUpdate(){
        console.log('componentWillUpdate');
    }
    componentDidUpdate(){
        console.log('componentDidUpdate')
    }
    //組件從頁(yè)面中移除前自動(dòng)執(zhí)行
    componentWillUnmount(){
    }
}
export default Demo

 子組件Demo2

import React,{Component} from 'react';
class Demo2 extends Component{
    componentWillReceiveProps(){
        console.log('componentWillReceiveProps');
    }
    render(){
        const {number} = this.props;
        return (<div>{number}</div>);
    }
}
export default Demo2;

當(dāng)子組件接收參數(shù)發(fā)生變化時(shí),就會(huì)執(zhí)行componentWillReceiveProps函數(shù),然后執(zhí)行shouldComponentUpdate函數(shù),返回值為true時(shí)依次執(zhí)行componentWillUpdate,render,componentDidUpdate

移除

當(dāng)組件從頁(yè)面移除時(shí)自動(dòng)執(zhí)行componentWillUnmount函數(shù),我們先定義一個(gè)路由

import React from 'react';
import ReactDom from 'react-dom';
import TodoList from './TodoList';
import {BrowserRouter,Routes,Route} from 'react-router-dom';
import ButtonTest from './ButtonTest';
import NewButton from './NewButton';
import Demo from './Demo';
class Entry extends React.Component{
    render(){
        return (
            <BrowserRouter>
                <Routes>
                    {/*{<Route path='/todoList' element={<TodoList/>}/>}*/}
                    {<Route path='/buttonTest' element={<ButtonTest/>}/>}
                    {<Route path='/newButton' element={<NewButton/>}/>}
                    <Route path='/Demo' element={<Demo/>}/>
                </Routes>
            </BrowserRouter>
        )
    }
}
ReactDom.render(<Entry/>,document.getElementById('root'));

從button組件跳轉(zhuǎn)到list組件,button從頁(yè)面移除時(shí)可觀察到自動(dòng)執(zhí)行了componentWillUnmount函數(shù)

import React,{Component} from 'react';
import { Button } from 'antd';
import {Link} from 'react-router-dom';
class NewButton extends Component{
    render(){
        return (
            <Link to='/buttonTest'>
                <Button type="primary">Primary</Button>
            </Link>
        );
    }
    //組件從頁(yè)面中移除前自動(dòng)執(zhí)行
    componentWillUnmount(){
        console.log('componentWillUnmount-----------');
    }
}
export default NewButton;
import React,{Component} from 'react';
import { List, Avatar } from 'antd';
const data = [
    {
        title: 'Ant Design Title 1',
    },
    {
        title: 'Ant Design Title 2',
    },
    {
        title: 'Ant Design Title 3',
    },
    {
        title: 'Ant Design Title 4',
    },
];
class ButtonTest extends Component{
    render(){
        return (
            <List
                itemLayout="horizontal"
                dataSource={data}
                renderItem={item => (
                    <List.Item>
                        <List.Item.Meta
                            avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
                            title={<a  rel="external nofollow" >{item.title}</a>}
                            description="Ant Design, a design language for background applications, is refined by Ant UED Team"
                        />
                    </List.Item>
                )}
            />
        );
    }
}
export default ButtonTest;

執(zhí)行結(jié)果

以上就是React的生命周期函數(shù)初始掛載更新移除詳解的詳細(xì)內(nèi)容,更多關(guān)于React 生命周期函數(shù) 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • React虛擬列表的實(shí)現(xiàn)

    React虛擬列表的實(shí)現(xiàn)

    在開(kāi)發(fā)過(guò)程中,總是遇到很多列表的顯示。當(dāng)上數(shù)量級(jí)別的列表渲染于瀏覽器,終會(huì)導(dǎo)致瀏覽器的性能下降,你可以選擇其他方式避免,本文就介紹了虛擬列表來(lái)解決這個(gè)問(wèn)題
    2021-05-05
  • Remix后臺(tái)開(kāi)發(fā)之remix-antd-admin配置過(guò)程

    Remix后臺(tái)開(kāi)發(fā)之remix-antd-admin配置過(guò)程

    這篇文章主要為大家介紹了Remix后臺(tái)開(kāi)發(fā)之remix-antd-admin配置過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Redux thunk中間件及執(zhí)行原理詳細(xì)分析

    Redux thunk中間件及執(zhí)行原理詳細(xì)分析

    redux的核心概念其實(shí)很簡(jiǎn)單:將需要修改的state都存入到store里,發(fā)起一個(gè)action用來(lái)描述發(fā)生了什么,用reducers描述action如何改變state tree,這篇文章主要介紹了Redux thunk中間件及執(zhí)行原理分析
    2022-09-09
  • react native之ScrollView下拉刷新效果

    react native之ScrollView下拉刷新效果

    這篇文章主要為大家詳細(xì)介紹了react native之ScrollView下拉刷新效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 基于React.js實(shí)現(xiàn)兔兔牌九宮格翻牌抽獎(jiǎng)組件

    基于React.js實(shí)現(xiàn)兔兔牌九宮格翻牌抽獎(jiǎng)組件

    這篇文章主要為大家詳細(xì)介紹了如何基于React.js實(shí)現(xiàn)兔兔牌九宮格翻牌抽獎(jiǎng)組件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-01-01
  • React中useEffect函數(shù)的使用詳解

    React中useEffect函數(shù)的使用詳解

    useEffect是React中的一個(gè)鉤子函數(shù),用于處理副作用操作,這篇文章主要為大家介紹了React中useEffect函數(shù)的具體用法,希望對(duì)大家有所幫助
    2023-08-08
  • 解決React報(bào)錯(cuò)`value` prop on `input` should not be null

    解決React報(bào)錯(cuò)`value` prop on `input` should&

    這篇文章主要為大家介紹了React報(bào)錯(cuò)`value` prop on `input` should not be null解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React中useState和useEffect的用法詳解

    React中useState和useEffect的用法詳解

    Hooks?發(fā)布之后,函數(shù)組件能擁有自己的?state,React提供了很多內(nèi)置的Hooks,這篇文章就來(lái)和大家介紹一下useState?和?useEffect的使用,需要的可以參考一下
    2023-06-06
  • webpack入門(mén)+react環(huán)境配置

    webpack入門(mén)+react環(huán)境配置

    webpack是一個(gè)前端資源模塊化管理和打包工具,說(shuō)白了就是方便我們管理自己的常用的一些代碼,比如你開(kāi)發(fā)中用到sass以及jade同時(shí)用到es6,開(kāi)發(fā)時(shí)你不可能改動(dòng)某個(gè)地方就挨個(gè)命令去轉(zhuǎn)換再到瀏覽器去看效果,那樣效率是非常低的。所以webpack幫我們省去了那些多余的步驟。
    2017-02-02
  • React class和function的區(qū)別小結(jié)

    React class和function的區(qū)別小結(jié)

    Class組件和Function組件是React中創(chuàng)建組件的兩種主要方式,本文主要介紹了React class和function的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10

最新評(píng)論