【问题标题】:How to use connect() and withStyles() for class component in React?如何在 React 中将 connect() 和 withStyles() 用于类组件?
【发布时间】:2020-02-25 13:16:33
【问题描述】:

React 中类组件如何使用 connect() 和 withStyles()?

const useStyles = makeStyles(theme => ({...});
const styles = useStyles();

class MyComponent extends React.Component {
    ...

    render() {
      return(<div className={...}>Stackoverflow</div>)
    }
}

const mapStateToProps = state => ({...});

export default connect(mapStateToProps, null)(withStyles(styles)(MyComponent))

【问题讨论】:

  • 你用过redux-thunk吗

标签: javascript reactjs react-redux material-ui


【解决方案1】:

你也需要使用 compose

import { bindActionCreators, compose } from 'redux';

export default compose(
  connect(
    mapStateToProps,
    mapDispatchToProps, // or put null here if you do not have actions to dispatch
  ),
  withStyles(styles),
)(YourComponent);

【讨论】:

  • 我有一个新问题
  • 新问题是什么?
  • 添加有问题
  • 您添加的问题与问题标题中的问题非常不同。在我看来,您试图在组件内部没有 mapDispatchToProps 的情况下分派一个函数。如果我的回答解决了您在 connect 中遇到的问题,我建议您将其标记为已解决并打开带有调度错误的新问题
猜你喜欢
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 2019-07-27
  • 2019-06-14
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多