哪个道具是由`react-redux`的`connect()`注入的?

问题描述 投票:0回答:1

看起来像official docsreact-redux没有提供由connect()注入的道具的完整列表。

connect()的返回是一个包装函数,它接受你的组件并返回一个包装器组件,它带有它注入的附加支持。

mapDispatchToPropsmapStateToProps添加的道具旁边的额外道具是什么。例如,dispatch prop被注入所有组件,但未在文档中指定。

有没有像dispatch那样被注射的其他道具?这种行为在将来的版本中是否会稳定?

如果注射dispatch。我也希望注射getState

redux react-redux documentation
1个回答
0
投票

connect()包装函数没有道具注入只是发送和不听商店。

export default connect()(YourComponent)

对代码https://github.com/reduxjs/react-redux/tree/master/src/connect做一些研究,你会在两个文件/函数中找到答案:

https://github.com/reduxjs/react-redux/blob/master/src/connect/mapDispatchToProps.js

export function whenMapDispatchToPropsIsMissing(mapDispatchToProps) {
  return !mapDispatchToProps
    ? wrapMapToPropsConstant(dispatch => ({ dispatch })) // ***** inject dispatch
    : undefined
}

https://github.com/reduxjs/react-redux/blob/master/src/connect/mapStateToProps.js

export function whenMapStateToPropsIsMissing(mapStateToProps) {
  return !mapStateToProps
    ? wrapMapToPropsConstant(() => ({})) // ***** empty object
    : undefined
}
© www.soinside.com 2019 - 2024. All rights reserved.