react-redux connect pattern to avoid unknown props warning with dispatch?

时间:2016-08-31 17:09:25

标签: react-redux

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.

1 个答案:

答案 0 :(得分:2)

我刚刚解决了同样的问题。 dispatch道具之所以存在,是因为您在致电mapDispatchToProps时没有提供connect()。因此,您可以提供一个虚拟函数来停止添加该道具:

export default connect( mapStateToProps, () => ({}) )(MyPage)