成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

React useEffect Hooks 傳遞不同參數有哪些執行規則和返回方式

開發 前端
在使用了useState或是useEffect這樣的hooks之后,每次組件在render的時候都生成了一份本次render的state、function、effects,這些與之前或是之后的render里面的內容都是沒有關系的。

Effect Hook 可以讓你在函數組件中執行副作用操作,這里提到副作用,什么是副作用呢,就是除了狀態相關的邏輯,比如網絡請求,監聽事件,查找 dom。

可以這樣說,在使用了useState或是useEffect這樣的hooks之后,每次組件在render的時候都生成了一份本次render的state、function、effects,這些與之前或是之后的render里面的內容都是沒有關系的。而對于class component來說,state是一種引用的形式。這就造成了二者在一些表現上的不同。

只要是副效應,都可以使用useEffect()引入。它的常見用途有下面幾種。

  • 獲取數據(data fetching)
  • 事件監聽或訂閱(setting up a subscription)
  • 改變 DOM(changing the DOM)
  • 輸出日志(logging)

副效應是隨著組件加載而發生的,那么組件卸載時,可能需要清理這些副效應。

useEffect()允許返回一個函數,在組件卸載時,執行該函數,清理副效應。如果不需要清理副效應,useEffect()就不用返回任何值。

使用useEffect()時,有一點需要注意。如果有多個副效應,應該調用多個useEffect(),而不應該合并寫在一起。

一、參數規則

1.可選的

2.數組類型

3.值為state或者props

二、不同的參數和返回

1.不傳參數

默認的行為,會每次 render 后都執行,一般表單控制中使用。

類似于類組件中的componentDidMoount以及componentDidUpdate。

useEffect(() => {
console.log('useEffect with no dependency')
})

2.空數組

傳入第二個參數,每次 render 后比較數組的值沒變化,不會在執行。

類似于類組件中的 componentDidMount。

useEffect(() => {
console.log('useEffect with empty dependency')
}, [])

3.有一個或者多個值得數組

傳入第二個參數,只有一個值,比較該值有變化就執行

傳入第二個參數,有2個值的數組,會比較每一個值,有一個不相等就執行;

類似于類組件中的componentDidUpdate;

useEffect(() => {
console.log('useEffect widh specify dependency')
}, [state, props])

4.返回一個函數

返回時傳遞一個函數進行卸載,在組件卸載時候調用;

類似于類組價中componentWillUnmout。

useEffect(() => {
console.log('useEffect widh specify callback');   
return () => {     
console.log('useEffect with specify callback');   
}
})

如果你熟悉 React class 的生命周期函數,你可以把 useEffect Hook 看做componentDidMount,componentDidUpdate 和 componentWillUnmount 這三個函數的組合。

class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
componentDidMount() {
document.title = `You clicked ${this.state.count} times`;
}
componentDidUpdate() {
document.title = `You clicked ${this.state.count} times`;
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}

使用 Hook 的示例

import React, { useState, useEffect } from 'react';

function Example() {
const [count, setCount] = useState(0);

useEffect(() => {
document.title = `You clicked ${count} times`;
});

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}

默認情況下,它在第一次渲染之后和每次更新之后都會執行。你可能會更容易接受 effect 發生在“渲染之后”這種概念,不用再去考慮“掛載”還是“更新”。React 保證了每次運行 effect 的同時,DOM 都已經更新完畢。

數據獲取,設置訂閱以及手動更改 React 組件中的 DOM 都屬于副作用。有些副作用可能需要清除,所以需要返回一個函數,比如掛載時設置定時器,卸載時取消定時器。

class Example extends Component {
constructor (props) {
super(props);
this.state = {
count: 0
}
}
componentDidMount() {
this.id = setInterval(() => {
this.setState({count: this.state.count + 1})
}, 1000);
}
componentWillUnmount() {
clearInterval(this.id)
}
render() {
return <h1>{this.state.count}</h1>;
}
}

使用 Hook 的示例

function Example() {
const [count, setCount] = useState(0);

useEffect(() => {
const id = setInterval(() => {
setCount(c => c + 1);
}, 1000);
return () => clearInterval(id);
}, []);

return <h1>{count}</h1>
}
責任編輯:武曉燕 來源: 今日頭條
相關推薦

2021-03-18 08:00:55

組件Hooks React

2022-08-21 09:41:42

ReactVue3前端

2022-09-23 10:25:00

VueReact

2022-09-19 19:51:30

ReactuseEffect

2019-03-13 10:10:26

React組件前端

2019-08-20 15:16:26

Reacthooks前端

2023-12-25 15:40:55

React開發

2023-03-29 23:23:00

MyBatis參數框架

2023-11-23 12:47:03

C++函數參數

2024-02-05 21:48:25

VueReactHooks

2021-07-01 07:51:45

React事件綁定

2011-05-18 10:52:51

java編碼規范

2023-11-06 08:00:00

ReactJavaScript開發

2021-07-05 11:06:11

組件React通信

2024-01-23 09:51:11

編程工具

2022-03-31 17:54:29

ReactHooks前端

2017-12-28 08:38:32

SAP HANA備份方式

2020-09-19 17:46:20

React Hooks開發函數

2022-07-18 09:01:58

React函數組件Hooks

2020-10-28 09:12:48

React架構Hooks
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩欧美在线视频 | 91在线观看免费 | 亚洲高清av在线 | 成人免费淫片aa视频免费 | 中文字幕第一页在线 | avhd101在线成人播放 | 狠狠干天天干 | 精品久久久久久久久亚洲 | 黄色一级大片在线观看 | 亚洲a网| 亚洲国产精品99久久久久久久久 | 91亚洲精品国偷拍自产在线观看 | 在线观看亚洲专区 | 91精品国产一二三 | 国产精品欧美一区二区三区 | 一本色道精品久久一区二区三区 | 国产精品视频一区二区三区不卡 | 日韩中文字幕 | 久久综合国产精品 | 精品91 | 国产精品色婷婷久久58 | 久久精品国产久精国产 | 91久久电影 | 天堂中文av| 亚洲视频一| 亚洲国产精品一区二区三区 | 亚洲欧美另类在线 | 国产在线视频一区二区 | 成人性生交a做片 | 一级黄在线观看 | 久久激情五月丁香伊人 | 日韩在线精品 | 亚洲一区在线免费观看 | 国产精品1区2区 | 亚洲精品一区二区网址 | 巨大黑人极品videos精品 | 国产精品色av | 中文字幕一级毛片 | 精品视频在线免费观看 | 99日韩| 国产情品 |