programing

React Material UI - 여러 고차

batch 2023. 4. 6. 21:21
반응형

React Material UI - 여러 고차

레덕스 커넥터가 있는 머티리얼-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

시도했습니다.

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

해결방법은?

이것만 먹어봐

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

여기서 App은 컴포넌트입니다.난 괜찮아.

material-ui docs 사이트, 특히 AppFrame 컴포넌트에서 어떻게 처리되고 있는지 살펴보십시오.

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

그들은 이것을 하기 위해 Recompose를 사용하고 있다.

당신의 경우, 다음과 같습니다.

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

없이.recompose또는compose:

Cart = withStyles(styles, {name: 'Cart'})(Cart);
export default reduxConnector(Cart);

설치하다npm install recompose또는yarn add recompose

그리고 당신의 수출 섹션에 있습니다.

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

매장을 수출하는 것을 잊어버렸습니다.

이건 내게 딱 맞는군

export default connect(mapStateToProps)((withStyles(styles)(ComponentNameToExport)));

아래를 사용하셔도 됩니다.WithStyles와 connect는 모두 상위 컴포넌트였기 때문에

export default withStyles(styles, {name: 'Cart'})(connect(mapStateToProps, mapDispatchToProps), Cart);

컴포넌트 완성

    import React from "react";
    import { makeStyles } from "@material-ui/core/styles";
    import ExpansionPanel from "@material-ui/core/ExpansionPanel";
    import ExpansionPanelSummary from "@material-ui/core/ExpansionPanelSummary";
    import ExpansionPanelDetails from "@material-ui/core/ExpansionPanelDetails";
    import Typography from "@material-ui/core/Typography";
    import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
    import { withStyles } from "@material-ui/core/styles";
    import { connect } from "react-redux";
    import { fetchPosts } from "../../store/actions/postActions";
    import PropTypes from "prop-types";

    const useStyles = theme => ({
        root: {
            marginLeft: 250,
            marginRight: 10
        },
        heading: {
            fontSize: "1rem",
            fontWeight: theme.typography.fontWeightRegular
        }
    });

    class SimpleExpansionPanel extends React.Component {
        UNSAFE_componentWillMount() {
            this.props.fetchPosts();
        }

        UNSAFE_componentWillReceiveProps(nextProps) {
            if (nextProps.newPost) {
                this.props.postsa.unshift(nextProps.newPost);
            }
        }

        render() {
            const { classes } = this.props;
            const postItem = this.props.postsa.map(post => (
                <ExpansionPanel key={post.id}>
                    <ExpansionPanelSummary
                        expandIcon={<ExpandMoreIcon />}
                        aria-controls="panel1a-content"
                        id="panel1a-header">
                        <Typography className={classes.heading}>{post.title}</Typography>
                    </ExpansionPanelSummary>
                    <ExpansionPanelDetails>
                        <Typography>{post.body}</Typography>
                    </ExpansionPanelDetails>
                </ExpansionPanel>
            ));
            return <div className={classes.root}>{postItem}</div>;
        }
    }

    SimpleExpansionPanel.propTypes = {
        fetchPosts: PropTypes.func.isRequired,
        postsa: PropTypes.array.isRequired,
        newPost: PropTypes.object
    };

    const mapStateToProps = state => ({
        postsa: state.postss.items,
        newPost: state.postss.item
    });

    export default connect(
        mapStateToProps,
        { fetchPosts }
    )(withStyles(useStyles)(SimpleExpansionPanel));

언급URL : https://stackoverflow.com/questions/45704681/react-material-ui-export-multiple-higher-order-components

반응형