dispatch
is automatically passed as a prop when you use connect(mapStateToProps)(Component)
. I'm not always using dispatch
in my connected components and React 15.2 throws a warning if my connected component passes it along to its children.
Simple example:
const Color = ({ color, ...props }) => <div {...props}>{color}</div>;
const CurrentColor = connect(getColorFromState)(Color);
Now if I use the CurrentColor
component anywhere it will throw an error because dispatch
is passed along. Is there a pattern I can use to avoid this? The only thing I could come up with was connect(mapStateToProps, () => ({}))(Component)
to override passing dispatch
.
答案 0 :(得分:2)
我刚刚解决了同样的问题。 dispatch
道具之所以存在,是因为您在致电mapDispatchToProps
时没有提供connect()
。因此,您可以提供一个虚拟函数来停止添加该道具:
export default connect(
mapStateToProps,
() => ({})
)(MyPage)