W
W
Wood Walker2019-02-05 12:54:51
React
Wood Walker, 2019-02-05 12:54:51

Am I writing redux HOC correctly?

I decided to try to write a redux HOC, but apparently I'm somehow not connecting to the store correctly.
The HOCa code is like this

import React from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';

const ProductsAndCustomers = Component => (
  class extends React.Component {
  // методы
    render() {
      return <Component/>;
    }
  }
)

const mapStateToProps = state => {
    return {
      ProductsAndCustomers: state
    }
}

const mapDispatchToProps = dispatch => {
  return {
    onRequestProducts: () => dispatch()
  };
}

// export default connect(
//   mapStateToProps,
//   mapDispatchToProps,
// )(ProductsAndCustomers);
// Пробовал таким образом, но видимо из-за того, что ProductsAndCustomer, не class, он ругается
const composeProductsAndCustomers = compose(
  connect(mapDispatchToProps, mapStateToProps),
  ProductsAndCustomers
);

export default composeProductsAndCustomers;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2019-02-05
@davidnum95

const withProductsAndCustomers = Component => {
 class ProductsAndCustomers extends React.Component {
  // методы
    render() {
      return <Component {...this.props}/>;
    }
  }
  return connect(mapStateToProps, mapDispatchToProps)(ProductsAndCustomers);
}

const mapStateToProps = state => {
    return {
      ProductsAndCustomers: state
    }
}

const mapDispatchToProps = dispatch => {
  return {
    onRequestProducts: () => dispatch()
  };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question