React Material UI - 导出多个更高阶的组件

ssu*_*hat 35 reactjs redux material-ui react-redux

我坚持用redux连接器导出material-ui样式.这是我的代码:

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
Run Code Online (Sandbox Code Playgroud)

我试过了:

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)".
Run Code Online (Sandbox Code Playgroud)

有解决方案吗

Ale*_*iro 58

试试吧

export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(App));
Run Code Online (Sandbox Code Playgroud)

应用程序是您的组件.这对我来说可以.

  • 很简单,效果很好 (2认同)

Ken*_*ory 31

看一下如何在material-ui docs网站中处理它,特别是在AppFrame组件中:

export default compose(
  withStyles(styles, {
    name: 'AppFrame',
  }),
  withWidth(),
  connect(),
)(AppFrame);
Run Code Online (Sandbox Code Playgroud)

他们正在使用重组来做到这一点.

所以在你的情况下,它将是:

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);
Run Code Online (Sandbox Code Playgroud)

  • @robertjewell完成了.链接特定提交的版本,以使其保持有效. (2认同)

sto*_*ter 10

没有recomposecompose:

Cart = withStyles(styles, {name: 'Cart'})(Cart);
export default reduxConnector(Cart);
Run Code Online (Sandbox Code Playgroud)


ssu*_*hat 9

安装npm install recomposeyarn add recompose

并在您的出口部分

export default compose(
    withStyles(styles, {
        name: 'App',
    }),
    connect(),
)(AppFrame);
Run Code Online (Sandbox Code Playgroud)

我忘了出口我的商店.