Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Material UI - Export multiple higher order components

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?

like image 442
ssuhat Avatar asked Aug 16 '17 03:08

ssuhat


People also ask

Can you export multiple components React?

Use named exports to export multiple components in React, e.g. export function A() {} and export function B() {} . The exported components can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

What is an hoc?

A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React's compositional nature. Concretely, a higher-order component is a function that takes a component and returns a new component.

How do you share logic across components in React?

If we want to use the same logic in another component we can start by extracting any logic that component requires into a hook in a separate file. The purpose of this hook is to provide any state and the functions a component requires to interact with that state.


1 Answers

Just try this

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

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

like image 74
Alex Moleiro Avatar answered Sep 22 '22 16:09

Alex Moleiro