简体   繁体   中英

How to use connect() and withStyles() for class component in React?

How to use connect() and withStyles() for class component in React?

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))

You need to use compose as well

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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