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

react-redux是react插件
將所有組件分成兩大類:UI組件和容器組件
安裝npm install react-redux -S
UI組件:

  1. 負責UI的呈現,不帶有任何業務邏輯
  2. 不使用this.state
  3. 所有數據都由this.props提供
  4. 不使用任何Redux的API,不需要使用store

容器組件:

  1. 負責管理數據和業務邏輯,不負責UI的呈現
  2. 帶有內部狀態
  3. 使用Redux的API

組件結構

  • 用容器組件包裹UI組件
  • 容器組件負責與外部的通信,將數據傳給UI組件
  • UI組件渲染出視圖
  • connect函數(來自react-redux插件):連接React組件與React store

關鍵字

  • connect
  • Provider

App修改為UI組件

  • index.js
import ReactDOM from 'react-dom'
import App from './App'
import { createStore } from 'redux'
import { counter } from './redux/reducers'
import { Provider } from 'react-redux'const store = createStore(counter)
ReactDOM.render(<Provider store={store}><App /></Provider>,document.getElementById('root')
)
  • App.jsx
import React, { Component, createRef } from 'react'
import { connect } from 'react-redux'
import * as actions from './redux/action'
class App extends Component {constructor(props) {super(props)this.selectRef = createRef()}compute = (method) => {const selectDom = this.selectRef.current,selectVal = Number(selectDom.value);// 使用傳入的props.actions this.props[method](selectVal)}render() {// 將App改造為UI組件const { count } = this.propsconsole.log('【app】', this)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></>)}
}
// 用connect函數處理App再暴露出去
export default connect(state => ({count: state // 這里的state是從redux容器中獲取到的數據}),{ ...actions }// 要解構出來!否則props里拿不到// 這樣即可在App這個UI組件里通過this.props.actions來使用
)(App)

connect方法接收2個參數

  1. 函數mapStateToProps,建立state對象到props對象的映射關系
    (redux store里的state可以通過UI組件的props獲取)
  2. mapDispatchToProps,建立一個store.dispatch方法到props對象的方法
    (redux里面action creators創建的函數可以通過props獲取)

在這里插入圖片描述

使用中間件

reducer:純函數,只承擔計算state的功能
view:與state意義對應
action:存放數據的對象,只能被別人操作?
同步:action發出后,reducer立即計算出state
異步:action發出后,過段時間再執行reducer
中間件:一個函數,對store.dispatch方法進行改造,在發出action和執行reducer兩步之間添加了其他功能

  • index.js(引入中間件)
import ReactDOM from 'react-dom'
import App from './App'
// 引入中間件
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import { counter } from './redux/reducers'
import { Provider } from 'react-redux'const store = createStore(counter, applyMiddleware(thunk))
ReactDOM.render(<Provider store={store}><App /></Provider>,document.getElementById('root')
)
  • reducer.js(只有同步方法)
export function counter(state = 0, action) {const { type, data } = actionswitch (type) {case 'add':return state + datacase 'minus':return state - datacase 'add_odd':if (data % 2 !== 0) {return state + data}// 這里面沒有異步方法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}
}
// 改造成異步action
export function add_delay(param) {return dispatch => {setTimeout(() => {dispatch(add(param))}, 1000)}
}

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

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

相關文章

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

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…