網(wǎng)站首頁 編程語言 正文
1. 概述
useReducer 這個 Hooks 在使用上幾乎跟 Redux 一模一樣,唯一缺點的就是無法使用 redux 提供的中間件。
使用 hook 函數(shù)后,一般情況下,一個組件組中的數(shù)據(jù)我們可以用 useReducer 來管理,而不是用 redux 來完成,redux 一般存儲公用數(shù)據(jù),而不是把所有的數(shù)據(jù)都存儲在里面,redux 它是一個單一數(shù)據(jù)源,如果存儲多個數(shù)據(jù)的話,性能會降低。
2. useReducer使用
import React, { useReducer } from 'react'
// useReducer 它就是一個小型的redux,它幾乎和redux是一樣的,也可以管理數(shù)據(jù)
// 初始化數(shù)據(jù)
const initState = {
count: 100
}
// reducer純函數(shù)中的state無需在定義函數(shù)時指定初始值,initState 會賦值給 reducer
// const reducer = (state,action)=>{}
// type, payload 是從 action 中結(jié)構(gòu)出來的
const reducer = (state, { type, payload }) => {
if (type === 'add') {
return { ...state, count: state.count + payload }
}
return state
}
const App = () => {
// state 它就是初始化后數(shù)據(jù)的對象,狀態(tài)
// dispatch 它就是用來發(fā)送指令讓reducer來修改state中的數(shù)據(jù)
// reducer它就是一個純函數(shù),用來修改initState初始化后數(shù)據(jù)狀態(tài)函數(shù)
// initState 初始化數(shù)據(jù)
const [state, dispatch] = useReducer(reducer, initState)
const addCount = () => {
// 數(shù)據(jù)以改變就會觸發(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個參數(shù),則第2個參數(shù)就沒有意義,它以第3個參數(shù)優(yōu)先,第3個參數(shù),惰性初始化,提升性能
// 第3參數(shù)它是一個回調(diào)函數(shù)且一定要返回一個對象數(shù)據(jù),當然你也可以直接返回一個值也可以,不建議
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ù)不會在一開始就進行初始化,而是在使用的時候再進行初始化。
3. 使用useReducer完成todolist列表功能
reducer.js:
// 導(dǎo)出初始化數(shù)據(jù)
export const initState = {
// 任務(wù)列表數(shù)據(jù)源
todos: []
}
// 導(dǎo)出用于操作當前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>
{/* 表單項 */}
<Form dispatch={dispatch} />
{/* 任務(wù)項 */}
<List dispatch={dispatch} todos={todos} />
</div>
)
}
export default Todo
表單項組件:
import React from 'react'
// 表單項組件
const Form = ({ dispatch }) => {
// 回車事件
const onEnter = evt => {
if (evt.keyCode === 13) {
const title = evt.target.value.trim()
// todo每項中的數(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ù)項組件:
import React from 'react'
// 任務(wù)項組件
const List = ({ todos, dispatch }) => {
const del = id => {
dispatch({ type: 'del', payload: id })
}
return (
<div>
<h3>任務(wù)項</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
原文鏈接:https://blog.csdn.net/weixin_45605541/article/details/127234734
相關(guān)推薦
- 2022-04-10 vscode快速輸出格式化console.log
- 2022-02-15 Linux系統(tǒng)設(shè)置tomcat開機自啟介紹_Linux
- 2022-07-20 spring boot 配置文件
- 2022-10-13 Golang執(zhí)行cmd命令行的方法_Golang
- 2022-07-28 C++圖文并茂講解繼承_C 語言
- 2024-03-19 Rust 中Self 關(guān)鍵字的兩種不同用法
- 2022-02-22 Linux系統(tǒng)下根目錄擴容介紹_Linux
- 2022-11-29 C#中各種泛型集合的使用方法總結(jié)_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支