網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
connect簡(jiǎn)介:
connect是react-redux兩個(gè)api中其中之一,在使用react-redux時(shí)起到了為redux中常用的功能實(shí)現(xiàn)了和react連接的建立
函數(shù)入口,以及需要傳入的參數(shù):
export default function connect(mapStateToProps, mapDispatchToProps, mergeProps, options = {}) {}
mapStateToProps:
傳入所有state,返回指定的state數(shù)據(jù)。
function mapStateToProps(state) {
return { todos: state.todos }
}
mapDispatchToProps:
傳入dispatch,返回使用綁定的action方法。
function mapDispatchToProps(dispatch) {
return bindActionCreators(Object.assign({}, todoActionCreators, counterActionCreators), dispatch)
}
mergeProps:
mergeProps如果不指定,則默認(rèn)返回 Object.assign({}, ownProps, stateProps, dispatchProps),顧名思義,mergeProps是合并的意思,將state合并后傳遞給組件
function mergeProps(stateProps, dispatchProps, ownProps) {
return Object.assign({}, ownProps, {
todos: stateProps.todos[ownProps.userId],
addTodo: (text) => dispatchProps.addTodo(ownProps.userId, text)
})
}
**options:**通過(guò)配置項(xiàng)可以更加詳細(xì)的定義connect的行為,通常只需要執(zhí)行默認(rèn)值。connect函數(shù)解析思路“
connect函數(shù)是核心既然是函數(shù),那就有返回值,connect()返回值是Connect組件,通俗點(diǎn)理解,使用connect可以把state和dispatch綁定到react組件,使得組件可以訪問(wèn)到redux的數(shù)據(jù)。
我們常用的寫法如下:
export default connect(mapStateToProps)(TodoApp)
原文鏈接:https://blog.csdn.net/qq_38980678/article/details/128798799
相關(guān)推薦
- 2022-04-12 Trailing spaces not allowed no-trailing-spaces
- 2022-06-08 Element-UI中selet下拉框無(wú)法回顯問(wèn)題
- 2022-08-26 Jquery實(shí)現(xiàn)下拉菜單案例_jquery
- 2022-03-16 .NET?6中的dotnet?monitor講解_實(shí)用技巧
- 2022-11-06 Android實(shí)現(xiàn)圓形圖片小工具_(dá)Android
- 2022-08-15 LCP創(chuàng)建bond接口
- 2022-05-09 python面向?qū)ο缶幊淘O(shè)計(jì)原則之單一職責(zé)原則詳解_python
- 2022-06-28 C#使用RestClient調(diào)用Web?API_C#教程
- 最近更新
-
- 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)程分支