1 redux初探、用react開發數值增值案例

含義

  • Redux是專門用作狀態管理的js庫,不是react插件庫
  • 可以用在react、angular、vue等項目中
  • 能集中式管理react應用中多個組件共享的狀態

使用

npx create-react-app redux1
cnpm install redux -S

關鍵字

  • store
  • reducer
  • action
  • subscribe

案例

1. 創建store、并傳入reducer 
import { createStore } from 'redux'
import { counter } from './redux/reducers'
const store = createStore(counter)
2. 使用dispatch
store.dispatch({type: '', // action的名字 data: '', // 傳給action的參數
})
3. reducer里處理數據
function counter(state = 0, action) {const { type, data } = action// 結構出dispatch的action type和參數,返回state
}
4. 監聽state,觸發更新
store.getState() // 獲取最新的state
store.subscribe(render) // 觸發ReactDOM.render
  • index.js 入口文件
import ReactDOM from 'react-dom'
import App from './App'
import { createStore } from 'redux'
import { counter } from './redux/reducers'
const store = createStore(counter)// state監聽,變化時執行傳入的方法
store.subscribe(render)
function render() {ReactDOM.render(<App store={store} />,document.getElementById('root'))
}
render()
  • reducers.js
export function counter(state = 0, action) {const { type, data } = actionconsole.log('reduce 觸發了方法', type)console.log('reduce 接收參數', data)switch (type) {case 'add':return state + datacase 'minus':return state - datacase 'add_odd':if (data % 2 !== 0) {return state + data}case 'add_delay':// 這里實現不了延時返回,可以拿到外面調用方法處去做延時調用setTimeout(() => {return state + data}, 1000)default:return state}
}
  • action.js
export function add (param){return{type: 'add', // 方法名data: param // 對應參數}
}
export function minus (param){return{type: 'minus',data: param}
}
export function add_odd (param){return{type: 'add_odd',data: param}
}
export function add_delay (param){return{type: 'add_delay',data: param}
}
  • App.jsx UI
import React, { Component, createRef } from 'react'
import * as actions from './redux/action'export default class App extends Component {constructor(props) {super(props)this.selectRef = createRef()}compute = (method) => {const selectDom = this.selectRef.current,selectVal = Number(selectDom.value),store = this.props.store;console.log('app store/method', store, method)store.dispatch(actions[method](selectVal))}render() {const count = this.props.store.getState()console.log('重新render了', count)return (<><h1>數值:{count}</h1><select ref={this.selectRef}><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>&nbsp;<button onClick={() => this.compute('add')}>+</button>&nbsp;<button onClick={() => this.compute('minus')}>-</button>&nbsp;<button onClick={() => this.compute('add_odd')}>奇數加</button>&nbsp;<button onClick={() => this.compute('add_delay')}>延遲加</button></>)}
}

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

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

相關文章

Python學習筆記_1_基礎_2:數據運算、bytes數據類型、.pyc文件(什么鬼)

Python學習筆記_1_基礎_2&#xff1a;數據運算、bytes數據類型、.pyc文件&#xff08;什么鬼&#xff09; 一、數據運算 Python數據運算感覺和C&#xff0c;Java沒有太大的差異&#xff0c;百度一大堆&#xff0c;這里就不想寫了。比較有意思的是三元運算&#xff0c;記得C和Ja…

LeetCode 8. String to Integer (atoi)

問題鏈接 LeetCode 8. String to Integer (atoi) 題目解析 字符串轉換成數字。 解題思路 個人認為題目并沒有完全講清楚題意。最初以為需要考慮多種情況&#xff0c;小數啊指數啊什么的&#xff0c;后面發現不是這樣的&#xff0c;這題只要關注字符數字問題以及范圍問題。 1.跳…

javascript --- 實戰中體會jsonp

準備:1.需要node環境,node環境配置 -> 百度搜索 “node環境配置” (網上太多了) node是否安裝成功指令如下: 創建如下頁面結構: html結構如下: // jsonp.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8">…

2 中間件的使用、異步action的創建

react-redux是react插件 將所有組件分成兩大類&#xff1a;UI組件和容器組件 安裝npm install react-redux -S UI組件&#xff1a; 只負責UI的呈現&#xff0c;不帶有任何業務邏輯不使用this.state所有數據都由this.props提供不使用任何Redux的API&#xff0c;不需要使用store …

PHP數組函數

整理了一份PHP開發中數組操作大全&#xff0c;包含有數組操作的基本函數、數組的分段和填充、數組與棧、數組與列隊、回調函數、排序、計算、其他的數組函數等。 一、數組操作的基本函數 數組的鍵名和值 array_values($arr); 獲得數組的值array_keys($arr); 獲得數組的鍵名ar…

Android Stadio 相關

這幾天&#xff0c;電腦壞了。重裝系統&#xff0c;慢慢的學到了很多Android stadio 的相關知識。總結一下吧&#xff1a; 1.gradle 編譯工具&#xff1a;在工程的gradle/wrapper/gradle–wrapper.properties 里面設置gradle 的版本。 distributionUrlhttps://services.grad…

1 State Hook

Hook&#xff0c;使用在函數組件中不要在循環&#xff0c;條件或嵌套函數中(if、switch、for)調用 Hook 1. 函數指向相同的引用 更新方式&#xff1a;函數組件中state變化時才重新渲染&#xff08;React使用Object.is比較算法來比較state&#xff09;&#xff1b;而類組件每次…

?第五篇? 隊列

隊列&#xff08;queue&#xff09;是只允許在一端進行插入操作&#xff0c;而在另一端進行刪除操作的線性表。 隊列是一種先進先出的&#xff08;First In First Out&#xff09;的線性表&#xff0c;簡稱FIFO。允許插入的一端為隊尾&#xff0c;允許刪除的一端為隊頭。隊列不…

es6 --- 數組的擴展

經常遇到對數組的操作…下面是《ES6標準入門》(第3版)中對數組擴展的(部分)描述: 擴展運算符(…): console.log(...[1,2,3]) // 1 2 3console.log(1, ... [2,3,4], 5) // 1 2 3 4 5擴展運算符代替數組的apply方法 // ES5 function f(x,y,z) {// ... } var args [1,2,3]; f.a…

算法 - 排序穩定性總結

排序方式 時間復雜度 空間復雜度 穩定性 平均情況 最壞情況 最好情況 插入排序 O(n^2) O(n^2) O(n) O(1) 穩定 希爾排序 O(n^1.3) O(1) 不穩定 冒泡排序 O(n^2) O(n^2) O(n) O(1) 穩定 快速排序 O(nlogn) O(n^2) O(nlogn) O(logn) 不穩定 選擇排…

node --- 實踐中理解跨域

經常可以見到.說解決跨域只要返回加上"Access-Control-Allow-Origin"頭部就行… 下面從實踐中一步一步的理解. 1.環境準備: 1. node.js (http://nodejs.cn/) 自行下載配置, 完畢后(cmd)輸入 node --version 若顯示版本號則代表成功// ps: node(中的npm)方便下載資源…

熟悉常用的Linux操作

cd命令&#xff1a;切換目錄 &#xff08;1&#xff09; 切換到目錄 /usr/local Cd /usr/local &#xff08;2&#xff09; 去到目前的上層目錄 Cd .. &#xff08;3&#xff09;回到自己的主文件夾 Cd ~ ls命令&#xff1a;查看文件與目錄 &#xff08;4&#xff09;查看…

2 Effect Hook

副作用&#xff1a;和外部有交互 引用外部變量調用外部函數修改dom、全局變量ajax計時器&#xff08;依賴window.setTimeout&#xff09;存儲相關 純函數&#xff1a;相同的輸入一定會得到相同的輸出 Effect Hook可以讓你在函數組件中執行副作用操作 類組件中處理副作用 在com…

【JUC】CountDownLatch

因為在調用端的異步中&#xff0c;需要調用其他多個服務獲取數據再匯總結果返回&#xff0c;所以用到了CountDownLatch CountDownLatch的概念 CountDownLatch是一個同步工具類&#xff0c;用來協調多個線程之間的同步&#xff0c;或者說起到線程之間的通信&#xff08;而不是用…

node --- Missing write access to 解決

今天在使用npm安裝animate.css時報錯… 大體原因是沒有對node_modules沒有寫的權限. 百度查到是要刪除對應的node_modules然后在安裝… 但是我并不想這樣做…想起前面我為了加快下載速度,好像使用的是cnpm… 于是我使用了nrm ls 查看當前使用的源 更換npm的源可以參考 https:…

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 { n…

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文件夾的相對…