網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
react-redux和redux的關(guān)系?
redux是react中進(jìn)行狀態(tài)管理的js庫(kù)(不是react插件),一般是管理多個(gè)組件中共享數(shù)據(jù)狀態(tài),和vue中的vuex是一樣的
react-redux是redux官方react綁定庫(kù),能夠使react組件從redux store中讀取數(shù)據(jù),并且向store分發(fā)actions以此來(lái)更新數(shù)據(jù),說(shuō)白了就是用于連接redux,它提供了connect和Provider兩個(gè)Api
react-redux基本使用
1.安裝
npm i react-redux@7 --save
盡量安裝8.0以下的版本,8.0以上會(huì)報(bào)錯(cuò),無(wú)法使用connect方法,提示hooks錯(cuò)誤,provider也無(wú)法使用
2.配置
首先,我們?cè)谌肟谖募信渲胷eact-redux的Provider方法,綁定store的redux對(duì)象數(shù)據(jù)
//導(dǎo)入react-redux的provier的組件
import { Provider } from "react-redux"
//...other
root.render(
<Provider store={store}>
<Routes />
</Provider>
);
?注意:Provider的store參數(shù)必須寫(xiě),如果不寫(xiě),他會(huì)報(bào)錯(cuò),他的原理跟我們使用context狀態(tài)樹(shù)中給provider傳遞的value值一樣
3.組件中使用
在我們要使用的組件中嘗試連接react-redux連接redux的數(shù)據(jù)
使用步驟
- 導(dǎo)入react-redux依賴包,引入connect方法
- connect有兩個(gè)函數(shù)參數(shù),mapStateToProps和mapDispatchToProps
- 把redux中state數(shù)據(jù)和action方法直接映射到組件props屬性中去
- 組件中可以直接通過(guò)props使用redux中的數(shù)據(jù),調(diào)用方法直接調(diào)用redux中的action的數(shù)據(jù)
- connect( mapStateToProps,mapDispatchToProps)(組件名)
代碼展示
import React from 'react';
import { connect } from 'react-redux';
export default function ReactRedux(props) {
console.log(props);
return (
<div>
<h3>react-redux</h3>
</div>
)
}
/把redux中的state數(shù)據(jù)映射到組件內(nèi)部的變量
const mapStateToProps=(state)=>{
console.log(state);
return {
...state
}
}
//把redux中action的操作,可以映射成為組件的props的內(nèi)部函數(shù)
const mapDispathToProps=(dispatch)=>{
return {
plus:()=>dispatch({type:'PLUS'})
}
}
//連接redux的操作的方法
ReactRedux=connect(mapStateToProps,mapDispathToProps)(ReactRedux)
connect方法執(zhí)行完成以后綁定當(dāng)前組件,復(fù)制完直接拋出即可
我們打印props中的數(shù)據(jù)會(huì)發(fā)現(xiàn)我們store中數(shù)據(jù)會(huì)作為props中屬性給我們打印出來(lái)
這樣,我們就可以使用props.屬性/方法進(jìn)行使用
原文鏈接:https://blog.csdn.net/qq_60976312/article/details/126273117
相關(guān)推薦
- 2023-04-04 iOS數(shù)據(jù)持久化UserDefaults封裝器使用詳解_IOS
- 2022-07-18 Redis服務(wù)器連接本地Linux所踩的坑
- 2022-07-20 react?diff?算法實(shí)現(xiàn)思路及原理解析_React
- 2022-04-02 Python字符串與正則表達(dá)式詳細(xì)介紹_python
- 2022-06-15 Go?interface?接口的最佳實(shí)踐經(jīng)驗(yàn)分享_Golang
- 2022-12-08 C語(yǔ)言程序如何求學(xué)生總成績(jī)和平均成績(jī)_C 語(yǔ)言
- 2023-01-05 Kotlin?高階函數(shù)與Lambda表達(dá)式示例詳解_Android
- 2022-07-09 鼠標(biāo)事件-事件對(duì)象
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支