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?
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.
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.
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.
Just try this
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(App));
Where App is your component. It works fine for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With