React Material UI - Export multiple higher order c

2019-01-13 18:49发布

I'm stuck on exporting material-ui styles with redux connector. Here is my code:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import Drawer from 'material-ui/Drawer';
import { withStyles } from 'material-ui/styles';
import PropTypes from 'prop-types';

const mapStateToProps = state => ({});

const reduxConnector = connect(mapStateToProps, null);
const styles = theme => {
    console.log(theme);
    return ({
        paper: {
            top: '80px',
            boxShadow: theme.shadows[9]
        },
    });
};

class Cart extends Component {

    render() {
        const { classes } = this.props;
        return (
            <Drawer
                open
                docked
                anchor="right"
                classes={{ paper: classes.paper }}
            >
                <p style={{ width: 250 }}>cart</p>

            </Drawer>
        );
    }
}

export default withStyles(styles, {name: 'Cart'})(Cart);
export default reduxConnector(Cart); // I want to add this

I've tried:

export default reduxConnector(withStyles(styles))(Cart); // return Uncaught TypeError: Cannot call a class as a function

export default withStyles(styles, {name: 'Cart'})(reduxConnector(Cart)); // return Uncaught Error: Could not find "store" in either the context or props of "Connect(Cart)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Cart)".

Any solution?

6条回答
爷的心禁止访问
2楼-- · 2019-01-13 19:27

Just try this

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

Where App is your component. It works fine for me.

查看更多
再贱就再见
3楼-- · 2019-01-13 19:29

install npm install recompose or yarn add recompose

and on your export section

export default compose(
    withStyles(styles, {
        name: 'App',
    }),
    connect(),
)(AppFrame);

and I forgot to export my store.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-13 19:29

You may use this below. As both withStyles and connect were higher order components

export default withStyles(styles, {name: 'Cart'})(connect(mapStateToProps, mapDispatchToProps), Cart);
查看更多
孤傲高冷的网名
5楼-- · 2019-01-13 19:36

Without recompose or compose:

Cart = withStyles(styles, {name: 'Cart'})(Cart);
export default reduxConnector(Cart);
查看更多
唯我独甜
6楼-- · 2019-01-13 19:40

Take a look at how it's being handled in the material-ui docs site, specifically in the AppFrame component:

export default compose(
  withStyles(styles, {
    name: 'AppFrame',
  }),
  withWidth(),
  connect(),
)(AppFrame);

They're using recompose to do this.

So in your case, it would be:

import React, { Component } from 'react';
import compose from 'recompose/compose';
import { connect } from 'react-redux';
import Drawer from 'material-ui/Drawer';
import { withStyles } from 'material-ui/styles';
import PropTypes from 'prop-types';

const styles = theme => {
  console.log(theme);
  return {
    paper: {
      top: '80px',
      boxShadow: theme.shadows[9],
    },
  };
};

const Cart = ({ classes }) =>
  <Drawer open docked anchor="right" classes={{ paper: classes.paper }}>
    <p style={{ width: 250 }}>cart</p>
  </Drawer>;

const mapStateToProps = state => ({});

export default compose(
  withStyles(styles, { name: 'Cart' }),
  connect(mapStateToProps, null)
)(Cart);
查看更多
劫难
7楼-- · 2019-01-13 19:51

You can try this:

 export default withStyles(styles, { withTheme: true }(connect(mapStateToProps,mapDispatchToProps)(App));
查看更多
登录 后发表回答