日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

react钩子_迷上了钩子:如何使用React的useReducer()

發(fā)布時(shí)間:2023/11/29 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 react钩子_迷上了钩子:如何使用React的useReducer() 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

react鉤子

So the React Conference just happened and as always something new happened. Hooks happened! The React team talked about suspense, lazy loading, concurrent rendering, and hooks :D.

因此,React會(huì)議剛剛發(fā)生,并且一如既往地發(fā)生了一些新情況。 鉤子發(fā)生了! React團(tuán)隊(duì)討論了懸念,延遲加載,并發(fā)渲染和鉤子 :D。

Now I’ll talk about my favorite hook useReducer and how you use it.

現(xiàn)在,我將討論我最喜歡的鉤子useReducer以及如何使用它。

In my PokemonInfo component, I have:

在我的PokemonInfo組件中,我有:

const [{ count, loading }, dispatch] = useReducer(reducer, initialState);

Which is equivalent to:

等效于:

const [state, dispatch] = useReducer(reducer, initialState); const { count, loading } = state;

So what is const [state, dispatch] = useReducer(param1, param2) Let’s first talk about array destructuing which is happening below.

那么什么是const [state, dispatch] = useReducer(param1, param2)首先讓我們談?wù)?strong>數(shù)組解構(gòu) 這正在發(fā)生在下面。

const [state, dispatch] = useReducer(initialState);

Here’s an example of array destructing:

這是數(shù)組銷(xiāo)毀的示例:

let myHeroes = ['Ant man', 'Batman']; // Mixing DC & Marvel :D let [marvelHero, dcHero] = myHeroes; // destructuring array /** * myHeroes[0] == marvelHero => is 'Ant man' * myHeroes[1] == dcHero => is 'Batman' */

So the method useReducer has two items in its array state and dispatch. Also the useReducer takes in two parameters: one is reducer the other is initial-state.

因此,方法useReducer的數(shù)組state和dispatch有兩個(gè)項(xiàng)目。 useReducer接受兩個(gè)參數(shù):一個(gè)是reducer ,另一個(gè)是initial-state 。

In the useReducer param reducer, I pass in:

在useReducer參數(shù)reducer ,我傳入:

const reducer = (state, action) => {switch (action.type) {case 'increment': {return { ...state, count: state.count + 1, loading: false };}case 'decrement': {return { ...state, count: state.count - 1, loading: false };}case 'loading': {return { ...state, loading: true };}default: {return state;}} };

What this does is it takes in two arguments. One is the current state of the reducer and the other is the action. The action.type decides how it will update the reducer and return a new state to us.

這樣做是有兩個(gè)參數(shù)的。 一個(gè)是減速器的當(dāng)前狀態(tài),另一個(gè)是動(dòng)作。 action.type決定如何更新減速器并向我們返回新?tīng)顟B(tài)。

So if the action.type === increment

因此,如果action.type === increment

case 'increment': { return { ...state, count: state.count + 1, loading: false }; }

…it will return the state, which will have its count updated to +1 and loading set to false. Also where it says state.count + 1 here the state is actually the previous state.

…它將返回狀態(tài),該狀態(tài)的計(jì)數(shù)將更新為+1并將加載設(shè)置為false 。 還在上面說(shuō)state.count + 1地方,這里的state實(shí)際上是先前的狀態(tài)。

In useReducer param initialState I pass in:

在useReducer參數(shù)initialState我傳入:

const initialState = { loading: false, count: 0 };

So if this is the initial state, the useReducer method returns two items from its array, state and dispatch. The state method is an object which has two keys count & loading that I destructure in my destructured array.

因此,如果這是初始狀態(tài),則useReducer方法從其數(shù)組中返回兩項(xiàng),即state和dispatch 。 state方法是一個(gè)具有兩個(gè)鍵count & loading的對(duì)象,我在已解構(gòu)數(shù)組中對(duì)其進(jìn)行了解構(gòu)。

So I destructure an array, and inside the array, I destructure an object on the first index of the array like below.

因此,我解構(gòu)了一個(gè)數(shù)組,然后在數(shù)組內(nèi)部,對(duì)一個(gè)對(duì)象進(jìn)行了解構(gòu),該對(duì)象位于數(shù)組的第一個(gè)索引上,如下所示。

const [{ count, loading }, dispatch] = useReducer(reducer, initialState);

Also I have a method called delay

我也有一種叫做delay的方法

// return true after 1500ms in time argument is passed to. const delay = (time = 1500) => { return new Promise(resolve => { setTimeout(() => { resolve(true); }, time); }); };

Now in my render method when I click the + button

現(xiàn)在在我的渲染方法中,當(dāng)我單擊+按鈕時(shí)

<button type="button" onClick={onHandleIncrement}>+</button>

the onHandleIncrement function is executed, which does the following:

執(zhí)行onHandleIncrement函數(shù),該函數(shù)執(zhí)行以下操作:

const onHandleIncrement = async () => { dispatch({ type: 'loading' }); await delay(500); dispatch({ type: 'increment' }); };

It initially sets loading to true, adds a delay of 500ms and then increments the counter. Now I know this is not a real world case example, but it explains the point as to how a reducer works.

最初將loading設(shè)置為true,添加500ms的延遲,然后遞增計(jì)數(shù)器。 現(xiàn)在,我知道這不是一個(gè)真實(shí)的案例,但是它解釋了減速器如何工作的要點(diǎn)。

Last thing:

最后一件事:

<p>Count {loading ? 'loading..' : count}</p>

If loading is true, I show Count loading.. else I show Count {value}.

如果loading為true,則顯示Count loading..否則顯示Count {value} 。

This is how it looks in the UI:

這是它在UI中的外觀:

I tried replicating Dan Abramov’s code that he showcased at the React Conference 2018. Here is the link to the code repository. Enjoy. :)

我嘗試復(fù)制Dan Abramov在React Conference 2018上展示的代碼。這是代碼庫(kù)的鏈接。 請(qǐng)享用。 :)

Kindly do note that hooks are in an alpha version of React, and are in no way advised to be used in production. But there is a strong possibility that they will become a huge part of the eco-system in the future. So you should start playing around with react hooks now. 請(qǐng)注意,掛鉤是React的alpha版本,絕不建議在生產(chǎn)中使用。 但是它們很可能在將來(lái)成為生態(tài)系統(tǒng)的重要組成部分。 因此,您現(xiàn)在應(yīng)該開(kāi)始使用react掛鉤。

翻譯自: https://www.freecodecamp.org/news/hooked-on-hooks-how-to-use-reacts-usereducer-2fe8f486b963/

react鉤子

總結(jié)

以上是生活随笔為你收集整理的react钩子_迷上了钩子:如何使用React的useReducer()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 青青草福利视频 | 午夜888 | 国产又色又爽又黄的 | a级片在线播放 | 色福利hd写真video | 国产做爰免费观看 | 天天摸天天舔天天操 | 91美女精品网站 | 国产91成人| 成人一卡二卡 | 成人高潮视频 | 老熟妻内射精品一区 | 999精品国产 | 欧美××××黑人××性爽 | 国产xxxxx | 影音先锋人妻啪啪av资源网站 | 中文在线资源天堂 | 国产精品久久久久久久久久久久久久久 | 久久av一区二区三区 | 男人天堂视频在线观看 | 九九av | 国产精品久久久久高潮 | 久草福利在线观看 | 日韩欧美中文字幕在线视频 | 亚洲福利专区 | 欧美日本黄色 | 国语播放老妇呻吟对白 | 色婷婷精品 | 波多野结衣有码 | 一级淫片免费 | 久久久久一级 | 国产精品入口麻豆 | 97成人资源 | 女人扒开屁股让男人捅 | 中文字幕无码人妻少妇免费 | 天天干夜夜干 | 欧美爱爱小视频 | 色图av | 男女在线视频 | 波多野结衣 久久 | www.一区| 欧美另类xxxx野战 | 臭脚猛1s民工调教奴粗口视频 | 免费毛片网站在线观看 | 少妇床戏av| 免费看欧美大片 | 国产精品中文字幕在线观看 | 精品国模 | 欧美一区二区三区四区视频 | 美女少妇毛片 | 精品人妻久久久久一区二区三区 | 亚洲影院在线播放 | 自拍三级视频 | 黑人高潮一区二区三区在线看 | 日本熟妇一区二区三区 | 肉感丰满的av演员 | 香蕉视频网站在线 | 日本免费一区二区三区视频 | 一区二区三区久久久久 | 一级片在线视频 | 最新av网址在线观看 | 五月天丁香网 | 国产极品视频在线观看 | 久久久在线视频 | 91丨porny在线 | 国产又好看的毛片 | 少妇视频 | 色戒av| 亚洲综合久久网 | 亚洲色图国产视频 | 国产在线18 | 成人在线观看视频网站 | 九九热最新视频 | 快色在线观看 | 日本网站黄色 | av秋霞 | 大色av| 色视频网 | 灌满闺乖女h高h调教尿h | 日本丰满bbwbbw厨房 | 熟妇人妻精品一区二区三区视频 | 欧美午夜激情影院 | 亚洲精品资源 | 台湾男男gay做爽爽的视频 | 一区二区三区四区五区av | 波多野结衣视频免费在线观看 | 2025av在线播放 | 欧美日韩在线视频一区二区 | 女人的天堂av在线 | 综合在线亚洲 | 日韩毛片在线观看 | av在线播放一区二区三区 | 色婷婷导航 | 波多野结衣电车痴汉 | 亚洲欧美另类在线视频 | 亚州久久久 | 一级片黑人 | 国产在线视频卡一卡二 | 幸福宝在线观看 |