3 useReducer及其實現

  • pureComponent
import { useState } from "react"
// useReducer,
// 統一調度
function reducer(state, action) {console.log('reducer接收參數', state, action)const { type } = actionswitch (type) {case 'add':return { num: state.num + 1 }case 'minus':return { num: state.num - 1 }}
}
function useReducer(reducer, initState) {// initState用于初始化const [state, setState] = useState(initState)const dispatch = (action) => {const newVal = reducer(state, action)setState(newVal)}return [state, dispatch]
}
export default function App() {// 參數:歸納函數,初始狀態const [state, dispatch] = useReducer(reducer, { num: 0 })return (<><p>數:{state.num}</p><button onClick={() => dispatch({ type: 'add' })}>+1</button><br /><button onClick={() => dispatch({ type: 'minus' })}>-1</button></>)
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250809.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250809.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250809.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Django 之 權限系統(組件)

參考: http://www.cnblogs.com/yuanchenqi/articles/7609586.html 轉載于:https://www.cnblogs.com/bigtreei/p/8564243.html

vue踩坑- 報錯npm ERR! cb() never called!

在vue項目中引入餓了么elementUI組件的步驟之中&#xff0c;出現以下的錯誤&#xff1a; D:\my-project-first>npm i element-ui -S Unhandled rejection RangeError: Maximum call stack size exceededill install loadIdealTreeat RegExp.test (<anonymous>)at D:\n…

maven之阿里云Maven鏡像的使用

Maven中央倉庫在國外&#xff0c;速度比較慢&#xff0c;所以我們采用國內的鏡像&#xff0c;速度回有質的提升。 配置下setting.xml <mirrors><mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/ne…

vue --- 使用animate.css實現動畫

1.下載animate.css npm install --save-dev animate.css// 注意你使用的源 nrm ls(若沒有改變可以忽略)2.導入animate.css <link rel"stylesheet" href"../node_modules/animate.css/animate.css"> // 注意你的當前文件和node_moudules文件夾的相對…

4 contextHook

類組件createContext、靜態屬性contextType 與函數組件useContext 的對比 import { Component, createContext, useContext } from react const AppContext createContext(0) class Foo extends Component {render() {return (<AppContext.Consumer>{value > (Foo: …

【leetcode 簡單】 第一百一十題 分發餅干

假設你是一位很棒的家長&#xff0c;想要給你的孩子們一些小餅干。但是&#xff0c;每個孩子最多只能給一塊餅干。對每個孩子 i &#xff0c;都有一個胃口值 gi &#xff0c;這是能讓孩子們滿足胃口的餅干的最小尺寸&#xff1b;并且每塊餅干 j &#xff0c;都有一個尺寸 sj 。…

基于openstack搭建百萬級并發負載均衡器的解決方案

最近&#xff0c;喜歡研究一些國外技術大咖們的文章&#xff0c;而這篇文章是基于openstack負載均衡器的解決方案&#xff0c;做的一些總結~希望能夠給小伙伴帶來一些靈感或者幫助。 openstack現有的負載均衡解決方案&#xff0c;無論是lbaas plugin還是octavia&#xff0c;后端…

5 useMemouseCallback

useMemo 優化渲染 現象 App每次重新執行時&#xff0c;render變化了&#xff0c;引用的render不是同一個函數 import React, { useState, } from "react"; const Foo props > {return <ul>{props.render()}</ul> } function App() {const [range…

vue --- 動畫執行的周期(動畫的鉤子函數)

如下8個: <transitionv-on:before-enter "beforeEnter"v-on:enter "enter"v-on:after-enter "afterEnter"v-on:enter-cancelled "enterCancelled"v-on:before-leave "beforeLeave"v-on:leave "leave"v-…

二分查找c++

相信對于二分查找的原理大家已經明白&#xff0c;接下來就是代碼實現了 1 #include <iostream>2 #include <cstdio>3 #include <algorithm>4 #include <cstring>5 #include <string>6 #include <cstdlib>7 8 using namespace std;9 10 in…

php獲取網址

1 #測試網址: http://localhost/blog/testurl.php?id52 3 //獲取域名或主機地址 4 echo $_SERVER[HTTP_HOST]."<br>"; #localhost5 6 //獲取網頁地址 7 echo $_SERVER[PHP_SELF]."<br>"; #/blog/testurl.php8 9 //獲取網址參數 10 echo …

6 useRef、useImperativeHandle

useRef在每次執行時返回的是同一個引用&#xff08;返回的ref對象在組件的整個生命周期內保持不變&#xff09;在函數組件中可以使用useRef和createRef但useRef性能比createRef好&#xff0c;快在類組件中&#xff0c;createRef是在初始化constructor時被賦值的&#xff08;執行…

vue --- 列表(v-for渲染)的各種神仙動畫效果

通過v-for生成的元素,使用transition包裹將只顯示第一條數據,此時需要使用transition-group包裹. <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-wid…

linux命令目錄

一、文件和目錄。&#xff08;文件目錄的增刪改查&#xff09; lspwdcdmkdirtouchrmdirlnddrmcpmvnlcattacmorelessheadtailstat###########################################grepawksed findlocatewhichwhereiswc ############################################dfdumountumoun…

vue --- 使用component的 :is屬性切換標簽頁

點擊對應的標簽,下面切換至對應的模板… // 說明 <component :is"name"></component> // 相當于把id為name的組件放到對應的位置總體代碼如下: <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"…

3-15 《元編程》第6章 3-16 hook method

Code That Writes Code 6.1 Coding your way to the weekend 6.2 Kernel#eval, Binding#eval Binding: Objects of class Binding(類Binding的對象) encapsulate &#xff08;密封&#xff09;the execution context at some particular place in the code and retain this c…

7 useLayoutEffect、useDebugValue

useEffect&#xff1a;dom完成渲染后執行 不傳參數&#xff0c;每次都會執行 傳空的依賴[]&#xff0c;只會執行一次 有依賴&#xff0c;依賴項變化會執行 useEffect實現動畫效果 import { useEffect, useRef, useState } from "react"const App () > {const [,…

【Linux】用戶與權限

追加用戶組 groupadd 用戶組名 追加新用戶 useradd -d 指定用戶目錄 -s 指定用戶使用shell -g 指定用戶組 -p 指定用戶密碼 用戶名 更改用戶 添加用戶到其他組 usermod -G 用戶組 用戶名 修改用戶密碼 passwd 用戶名 刪除用戶 userdel [參數] 用戶名參數&#xff1a;-f&#x…

es6 --- map的使用

思路: 1.使用一個map數組來保存nums1中出現的元素及其次數. 2.遍歷nums2.使用map的has方法來檢測nums2中的元素是否出現在map中,若出現則加入返回數組(retArr),且map數組中的次數減1 /*** param {number[]} nums1* param {number[]} nums2* return {number[]}*/ var intersect…

NOIP2005普及組第3題 采藥 (背包問題)

NOIP2005普及組第3題 采藥    時間限制: 1 Sec 內存限制: 128 MB提交: 50 解決: 23[提交][狀態][討論版][命題人:外部導入]題目描述 辰辰是個天資聰穎的孩子&#xff0c;他的夢想是成為世界上最偉大的醫師。為此&#xff0c;他想拜附近最有威望的醫師為師。醫師為了判斷他…