【发布时间】:2019-01-17 09:44:05
【问题描述】:
我真的无法理解 mapDispatchToProps 中 mapStateToProps 和 ownProps 的范围
const MyComponent = (props) => (
<button onClick={props.onClick}>{props.myProp} / {props.otherProp}</button>
)
const ConnctedMyComponent = connect(
(state) => ({
myProp: 'mapped',
otherProp: 'other'
}),
(dispatch, ownProps) => ({
onClick: () => {
console.log(ownProps);
}
})
)(MyComponent)
日志显示
{ myProp: 'original' }
- 为什么
myProp没有设置为mapped? - 为什么
otherProp不可用? - 按钮内容正确设置为
mapped / other - 这是我应该将映射道具传递给映射调度道具的方式吗?
const MyComponent = (props) => (
<button onClick={() => props.onClick(props.myProp)}>{props.myProp} / {props.otherProp}</button>
)
- 有没有办法将映射调度回调绑定到连接的组件(如访问映射存储属性)?
谢谢
【问题讨论】:
-
可能是减速器的错误?你能展示一下减速机吗?
-
请检查github.com/reduxjs/redux-devtools/issues/250,这正是您要找的
-
@DanielArtola 这里没有使用减速器,所以它只是映射“问题”
标签: javascript reactjs react-redux