亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React useReducer終極使用教程

 更新時(shí)間:2022年10月18日 14:18:46   作者:月光曬了很涼快  
useReducer是在react V16.8推出的鉤子函數(shù),從用法層面來(lái)說(shuō)是可以代替useState。相信前期使用過(guò) React 的前端同學(xué),大都會(huì)經(jīng)歷從class語(yǔ)法向hooks用法的轉(zhuǎn)變,react的hooks編程給我們帶來(lái)了絲滑的函數(shù)式編程體驗(yàn)

1. 概述

useReducer 這個(gè) Hooks 在使用上幾乎跟 Redux 一模一樣,唯一缺點(diǎn)的就是無(wú)法使用 redux 提供的中間件。

使用 hook 函數(shù)后,一般情況下,一個(gè)組件組中的數(shù)據(jù)我們可以用 useReducer 來(lái)管理,而不是用 redux 來(lái)完成,redux 一般存儲(chǔ)公用數(shù)據(jù),而不是把所有的數(shù)據(jù)都存儲(chǔ)在里面,redux 它是一個(gè)單一數(shù)據(jù)源,如果存儲(chǔ)多個(gè)數(shù)據(jù)的話,性能會(huì)降低。

2. useReducer使用

import React, { useReducer } from 'react'
// useReducer 它就是一個(gè)小型的redux,它幾乎和redux是一樣的,也可以管理數(shù)據(jù)
// 初始化數(shù)據(jù)
const initState = {
  count: 100
}
// reducer純函數(shù)中的state無(wú)需在定義函數(shù)時(shí)指定初始值,initState 會(huì)賦值給 reducer
// const reducer = (state,action)=>{}
// type, payload 是從 action 中結(jié)構(gòu)出來(lái)的
const reducer = (state, { type, payload }) => {
  if (type === 'add') {
    return { ...state, count: state.count + payload }
  }
  return state
}
const App = () => {
  // state 它就是初始化后數(shù)據(jù)的對(duì)象,狀態(tài)
  // dispatch 它就是用來(lái)發(fā)送指令讓reducer來(lái)修改state中的數(shù)據(jù)
  // reducer它就是一個(gè)純函數(shù),用來(lái)修改initState初始化后數(shù)據(jù)狀態(tài)函數(shù)
  // initState 初始化數(shù)據(jù)
  const [state, dispatch] = useReducer(reducer, initState)
  const addCount = () => {
    // 數(shù)據(jù)以改變就會(huì)觸發(fā) useReducer 中的 reducer 函數(shù)
    dispatch({ type: 'add', payload: 2 })
  }
  return (
    <div>
      <h3>App組件 -- {state.count}</h3>
      <button onClick={addCount}>++++</button>
    </div>
  );
}
export default App;

useReducer 的惰性初始化:

import React, { useReducer } from 'react'
const initState = {
  count: 100
}
const reducer = (state, { type, payload }) => {
  if (type === 'add') {
    return { ...state, count: state.count + payload }
  }
  return state
}
const App = () => {
  // initState 初始化數(shù)據(jù)
  // 如果useReducer它有第3個(gè)參數(shù),則第2個(gè)參數(shù)就沒有意義,它以第3個(gè)參數(shù)優(yōu)先,第3個(gè)參數(shù),惰性初始化,提升性能
  // 第3參數(shù)它是一個(gè)回調(diào)函數(shù)且一定要返回一個(gè)對(duì)象數(shù)據(jù),當(dāng)然你也可以直接返回一個(gè)值也可以,不建議
  const [state, dispatch] = useReducer(reducer, null, () => ({ count: 2000 }))
  const addCount = () => {
    dispatch({ type: 'add', payload: 2 })
  }
  return (
    <div>
      <h3>App組件 -- {state.count}</h3>
      <button onClick={addCount}>++++</button>
    </div>
  );
}
export default App;

注意:惰性初始化可以提升性能,惰性初始化使得數(shù)據(jù)不會(huì)在一開始就進(jìn)行初始化,而是在使用的時(shí)候再進(jìn)行初始化。

3. 使用useReducer完成todolist列表功能

reducer.js:

// 導(dǎo)出初始化數(shù)據(jù)
export const initState = {
  // 任務(wù)列表數(shù)據(jù)源
  todos: []
}
// 導(dǎo)出用于操作當(dāng)前todos任務(wù)列表的純函數(shù)
export const reducer = (state, { type, payload }) => {
  // [...state.todos, payload] 追加數(shù)據(jù)
  if ('add' === type) return { ...state, todos: [...state.todos, payload] }
  if ('del' === type) return { ...state, todos: state.todos.filter(item => item.id != payload) }
  return state
}

父組件(App.jsx):

import React from 'react';
import Todo from './Todo';
const App = () => {
  return (
    <div>
      <Todo />
    </div>
  );
}
export default App;

ToDo組件:

import React, { useReducer } from 'react'
import Form from './components/Form'
import List from './components/List'
// 導(dǎo)入 reducer 文件中的初始化數(shù)據(jù)和操作數(shù)據(jù)函數(shù)
import { initState, reducer } from './reducer'
const Todo = () => {
  const [{ todos }, dispatch] = useReducer(reducer, initState)
  return (
    <div>
      {/* 表單項(xiàng) */}
      <Form dispatch={dispatch} />
      {/* 任務(wù)項(xiàng) */}
      <List dispatch={dispatch} todos={todos} />
    </div>
  )
}
export default Todo

表單項(xiàng)組件:

import React from 'react'
// 表單項(xiàng)組件
const Form = ({ dispatch }) => {
  // 回車事件
  const onEnter = evt => {
    if (evt.keyCode === 13) {
      const title = evt.target.value.trim()
      // todo每項(xiàng)中的數(shù)據(jù)
      const todo = {
        id: Date.now(),
        title,
        done: false
      }
      dispatch({ type: 'add', payload: todo })
      evt.target.value = ''
    }
  }
  return (
    <div>
      <input onKeyUp={onEnter} />
    </div>
  )
}
export default Form

任務(wù)項(xiàng)組件:

import React from 'react'
// 任務(wù)項(xiàng)組件
const List = ({ todos, dispatch }) => {
  const del = id => {
    dispatch({ type: 'del', payload: id })
  }
  return (
    <div>
      <h3>任務(wù)項(xiàng)</h3>
      <hr />
      <ul>
        {todos.map(item => (
          <li key={item.id}>
            <span>{item.title}</span>
            <span onClick={() => del(item.id)}>刪除</span>
          </li>
        ))}
      </ul>
    </div>
  )
}
export default List

到此這篇關(guān)于React useReducer終極使用教程的文章就介紹到這了,更多相關(guān)React useReducer內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解如何使用React和MUI創(chuàng)建多選Checkbox樹組件

    詳解如何使用React和MUI創(chuàng)建多選Checkbox樹組件

    這篇文章主要為大家詳細(xì)介紹了如何使用 React 和 MUI(Material-UI)庫(kù)來(lái)創(chuàng)建一個(gè)多選 Checkbox 樹組件,該組件可以用于展示樹形結(jié)構(gòu)的數(shù)據(jù),并允許用戶選擇多個(gè)節(jié)點(diǎn),感興趣的可以了解下
    2024-01-01
  • React之Hooks詳解

    React之Hooks詳解

    這篇文章主要介紹了React hooks的優(yōu)缺點(diǎn)詳解,幫助大家更好的理解和學(xué)習(xí)使用React,感興趣的朋友可以了解下, 希望能夠給你帶來(lái)幫助
    2021-09-09
  • react拖拽組件react-sortable-hoc的使用

    react拖拽組件react-sortable-hoc的使用

    本文主要介紹了react拖拽組件react-sortable-hoc的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • React RenderProps模式超詳細(xì)講解

    React RenderProps模式超詳細(xì)講解

    render props是指一種在 React 組件之間使用一個(gè)值為函數(shù)的 prop 共享代碼的技術(shù)。簡(jiǎn)單來(lái)說(shuō),給一個(gè)組件傳入一個(gè)prop,這個(gè)props是一個(gè)函數(shù),函數(shù)的作用是用來(lái)告訴這個(gè)組件需要渲染什么內(nèi)容,那么這個(gè)prop就成為render prop
    2022-11-11
  • 更強(qiáng)大的React 狀態(tài)管理庫(kù)Zustand使用詳解

    更強(qiáng)大的React 狀態(tài)管理庫(kù)Zustand使用詳解

    這篇文章主要為大家介紹了更強(qiáng)大的React 狀態(tài)管理庫(kù)Zustand使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 詳解React中Fragment的簡(jiǎn)單使用

    詳解React中Fragment的簡(jiǎn)單使用

    這篇文章主要介紹了詳解React中Fragment的簡(jiǎn)單使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)我們學(xué)習(xí)React有一定的幫助,感興趣的小伙伴們可以參考一下
    2022-10-10
  • React18+TS通用后臺(tái)管理系統(tǒng)解決方案落地實(shí)戰(zhàn)示例

    React18+TS通用后臺(tái)管理系統(tǒng)解決方案落地實(shí)戰(zhàn)示例

    這篇文章主要為大家介紹了React18+TS通用后臺(tái)管理系統(tǒng)解決方案落地實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • react antd-mobile ActionSheet+tag實(shí)現(xiàn)多選方式

    react antd-mobile ActionSheet+tag實(shí)現(xiàn)多選方式

    這篇文章主要介紹了react antd-mobile ActionSheet+tag實(shí)現(xiàn)多選方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 修復(fù)Next.js中window?is?not?defined方法詳解

    修復(fù)Next.js中window?is?not?defined方法詳解

    這篇文章主要為大家介紹了修復(fù)Next.js中window?is?not?defined方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • react實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)信息

    react實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)信息

    這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10

最新評(píng)論