简体   繁体   English

为什么我不能将redux的connect与material-ui withStyles配合使用?

[英]Why I can't apply redux's connect with material-ui withStyles?

I'm trying to add withStyles() hook from material-ui in the redux container-component with connect() function via { compose } function from 'recompose' and get this error from recompose package: 我正在尝试通过'recompose'的{compose}函数通过connect()函数在redux容器组件的material-ui中添加withStyles()钩子,并从recompose包中获取此错误:

TypeError: Function.prototype.apply was called on #, which is a object and not a function TypeError:在#上调用了Function.prototype.apply,它是一个对象而不是一个函数

I ask for any help, I have already spent too much time on this 我需要任何帮助,我已经花了太多时间


import { withStyles } from '@material-ui/core/styles';
import { styles } from './styles';
import { compose } from 'recompose';
import { connect } from 'react-redux';

...

function mapStateToProps(state) {
  return {
    someVal: state.someVal,
  }
}

function mapDispatchToProps(dispatch) {
  return ({
    changeVal: () => {dispatch('CHANGE_VAL')}
  })
}

export default compose(
  withStyles(styles),
  connect(mapStateToProps, mapDispatchToProps)(App)
);


//if i do:

export default connect(mapStateToProps,mapDispatchToProps)(App)

//or:

export default withStyles(styles)(App)

//it's work. (just to clarify)

connect and withStyles are both HOC connectwithStyles都是HOC

a higher-order component is a function that takes a component and returns a new component. 高阶组件是接受组件并返回新组件的函数。

So you need to wrap App inside withStyles and connect 所以你需要用withStyles包装App并connect

export default connect(mapStateToProps,mapDispatchToProps)(withStyles(styles)(App))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM