react-redux多個(gè)組件數(shù)據(jù)共享的方法
多個(gè)組件數(shù)據(jù)共享
我們之前講解的一直都是只有一個(gè)組件需要向redux讀取狀態(tài),也就是Count這個(gè)求和組件。那么我們在實(shí)際使用redux的場景中,當(dāng)然是有很多組件一起共享數(shù)據(jù)才需要使用到redux進(jìn)行狀態(tài)管理啦,現(xiàn)在我們就來看看多個(gè)組件通過redux實(shí)現(xiàn)數(shù)據(jù)共享的場景吧~
現(xiàn)在我們創(chuàng)建一個(gè)Person組件,同樣的,Person組件的數(shù)據(jù)也交給redux管理。此時(shí),Count組件也可以從redux中讀取到Person組件的數(shù)據(jù),Person組件也可以從redux中讀取到Count組件之前存放在redux中的數(shù)據(jù)。是不是很方便呢?這就是redux集中式的狀態(tài)管理中的多個(gè)組件的數(shù)據(jù)共享。
項(xiàng)目結(jié)構(gòu):
src ├─App.jsx ├─index.js ├─redux | ├─constant.js | ├─store.js | ├─reducers | | ├─count.js | | └person.js | ├─actions | | ├─count.js | | └person.js ├─containers | ├─Person | | └index.jsx | ├─Count | | └index.jsx
項(xiàng)目展示:
??注意:Count組件部分內(nèi)容已在前幾篇文章中,在本文中注重的是新增的Person組件與之前的Count組件共享狀態(tài)。
1.首先在constant.js
中添加我們在Person組件中需要使用的類型:
export const INCREMENT = 'increment' export const DECREMENT = 'decrement' + export const ADD_PERSON = 'add_person'
該模塊是用于定義action對象中type類型的常量模塊,便于管理的同時(shí)避免程序員單詞拼寫出錯(cuò)。
2.編寫Person組件的action文件,用于創(chuàng)建action動(dòng)作對象以供Person組件使用:
/src/redux/actions/person.js
/* 該文件專門為Person組件生成action動(dòng)作對象 */ import { ADD_PERSON } from "../constant"; // 創(chuàng)建增加一個(gè)person的action動(dòng)作對象 export const createAddPersonAction = personObj => ({ type: ADD_PERSON, data: personObj })
3.編寫Person組件的reducer文件,用于創(chuàng)建一個(gè)為Person組件服務(wù)的reducer函數(shù)
/src/redux/reducers/person.js
/* 1.該文件用于創(chuàng)建一個(gè)為Person組件服務(wù)的reducer函數(shù) 2.reducer函數(shù)會(huì)接收到兩個(gè)參數(shù),分別為之前的狀態(tài)(prevState)和動(dòng)作對象(action) */ import { ADD_PERSON } from "../constant"; const initState = [{ id: 001, name: 'tom', age: 18 }] export default function personReducer(prevState = initState, action) { const { type, data } = action switch (type) { case ADD_PERSON: return [data, ...prevState] default: return prevState } }
4.redux若只為一個(gè)組件服務(wù),store內(nèi)部存儲(chǔ)的數(shù)據(jù)不需要任何的標(biāo)識。但是store中若存放了多個(gè)組件的狀態(tài),那么就需要用一個(gè)對象將所有的狀態(tài)囊括起來,每個(gè)狀態(tài)都是一組key:value值。
比如,Count組件存儲(chǔ)的狀態(tài)為:count:0
。Person組件存儲(chǔ)的狀態(tài)為:persons:[]
。
/src/redux/store.js
- import { legacy_createStore as createStore, applyMiddleware } from 'redux'; // 引入combineReducers,用于合并reducer + import { legacy_createStore as createStore, applyMiddleware, combineReducers } from 'redux'; import countReducer from './reducers/count' // 引入為Person組件服務(wù)的reducer + import personReducer from './reducers/person'; import thunk from 'redux-thunk' + // 合并reducer + const allReducer = combineReducers({ + count: countReducer, + persons: personReducer + }) // 暴露store - export default createStore(countReducer, applyMiddleware(thunk)) + export default createStore(allReducer, applyMiddleware(thunk))
在原先的store中只存放了一個(gè)Count組件的狀態(tài)數(shù)據(jù),現(xiàn)在新增了Person組件需要使用redux,那么就應(yīng)該對store.js進(jìn)行一些修改。
在store.js文件中,從redux中新引入combineReducers函數(shù)用于合并reducer;
引入為Person組件服務(wù)的reducer;
將原先的countReducer與新引入的personReducer合并,并且將合并后的allReducer傳遞給createStore函數(shù)作為第一個(gè)參數(shù),目的是將這兩個(gè)組件的狀態(tài)用一個(gè)對象包裹起來,再傳給store。
5.Person組件已經(jīng)與redux建立起了聯(lián)系,那么現(xiàn)在可以在Person組件中書寫Person的UI組件以及Person的容器組件(使用react-redux)。
import React, { Component } from 'react' import { nanoid } from 'nanoid' import { connect } from 'react-redux' import { createAddPersonAction } from '../../redux/actions/person' class Person extends Component { addPerson = () => { const name = this.nameNode.value const age = this.ageNode.value const personObj = { id: nanoid(), name, age } this.props.dispatchAddPerson(personObj) this.nameNode.value = '' this.ageNode.value = '' } render() { return ( <div> <h2>我是Person組件,上方組件求和為:{this.props.count}</h2> <input ref={currentNode => this.nameNode = currentNode} type="text" placeholder='輸入名字' /> <input ref={currentNode => this.ageNode = currentNode} type="text" placeholder='輸入年齡' /> <button onClick={this.addPerson}>添加</button> <ul> { this.props.personArr.map(personObj => { return <li key={personObj.id}>{personObj.name}---{personObj.age}</li> }) } </ul> </div> ) } } export default connect( state => ({ personArr: state.persons, count: state.count }), { dispatchAddPerson: createAddPersonAction } )(Person)
6.同時(shí)修改Count組件內(nèi)容,使Count組件可以顯示Person組件的人數(shù)。
<h2>我是Count組件,下方組件總?cè)藬?shù)為:{this.props.person}</h2> // // export default connect( state => ({ count: state.count, person: state.persons.length }), { jia: createIncrementAction, jian: createDecrementAction, jiaAsync: createIncrementAsyncAction } )(Count)
注意:關(guān)于connect()()函數(shù)詳解內(nèi)容,點(diǎn)擊:<react-redux>基本使用與優(yōu)化
總結(jié):
- 定義一個(gè)Person組件,和Count組件通過redux共享數(shù)據(jù);
- 為Person組件編寫:reducer、action,配置constant常量;
- 重點(diǎn):Person的reducer和Count的reducer要使用combineReducers進(jìn)行合并,合并后的總狀態(tài)是一個(gè)對象;
- 交給store的是總reducer。
到此這篇關(guān)于react-redux多個(gè)組件數(shù)據(jù)共享的方法的文章就介紹到這了,更多相關(guān)react-redux數(shù)據(jù)共享內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在React中用canvas對圖片標(biāo)注的實(shí)現(xiàn)
本文主要介紹了在React中用canvas對圖片標(biāo)注的實(shí)現(xiàn) ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05React Refs轉(zhuǎn)發(fā)實(shí)現(xiàn)流程詳解
Refs是一個(gè) 獲取 DOM節(jié)點(diǎn)或React元素實(shí)例的工具,在React中Refs 提供了一種方式,允許用戶訪問DOM 節(jié)點(diǎn)或者在render方法中創(chuàng)建的React元素,這篇文章主要給大家介紹了關(guān)于React中refs的一些常見用法,需要的朋友可以參考下2022-12-12TypeScript在React項(xiàng)目中的使用實(shí)踐總結(jié)
這篇文章主要介紹了TypeScript在React項(xiàng)目中的使用總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04在React項(xiàng)目中實(shí)現(xiàn)一個(gè)簡單的錨點(diǎn)目錄定位
錨點(diǎn)目錄定位功能在長頁面和文檔類網(wǎng)站中非常常見,它可以讓用戶快速定位到頁面中的某個(gè)章節(jié),本文講給大家介紹一下React項(xiàng)目中如何實(shí)現(xiàn)一個(gè)簡單的錨點(diǎn)目錄定位,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-09-09基于React Context實(shí)現(xiàn)一個(gè)簡單的狀態(tài)管理的示例代碼
本文主要介紹了基于React Context實(shí)現(xiàn)一個(gè)簡單的狀態(tài)管理的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07React?createRef循環(huán)動(dòng)態(tài)賦值ref問題
這篇文章主要介紹了React?createRef循環(huán)動(dòng)態(tài)賦值ref問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01