React redux connect with with styles and withrouter

荒凉一梦 提交于 2019-12-05 12:15:31

You will need to install recompose:

npm i -s recompose

Then in your component:

import compose from 'recompose/compose'


export default compose(
   withStyles(styles),
   connect(mapStateToProps, mapDispatchToProps)
)(withRouter(Dashboard))

What's the return value of withStyles(styles)? I suspect it is a "Higher Order Component" (HOC), which is a function that expects to be passed the React component to wrap, and returns a React component. If that is the case, then you really want your call to look like this:

export default withStyles(styles)(
    connect(mapStateToProps, mapDispatchToProps)(withRouter(Dashboard))
)

That code is pretty ugly, though, and will rapidly get worse as you add in more HOCs, which is why the Recompose suggestion is a much better way to go. (But I wanted to add in some more context so that readers an understand what was causing the problem in the OP.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!