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

时间:2019-10-30 09:16:59

标签: javascript reactjs react-redux material-ui

如何在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))

1 个答案:

答案 0 :(得分: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);