Switching products data from Redux to Context Api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React,{useContext} from 'react' | |
import PropTypes from 'prop-types' | |
import { connect } from 'react-redux' | |
import { addToCart } from '../actions' | |
import { getVisibleProducts } from '../reducers/products' | |
import ProductItem from '../components/ProductItem' | |
import ProductsList from '../components/ProductsList' | |
import {ProductsContext} from '../provider/products.provider' | |
const ProductsContainer = ({ addToCart }) => { | |
const {test, products} = useContext(ProductsContext) | |
console.log(test) | |
return ( | |
<ProductsList title="Products"> | |
{products.map(product => | |
<ProductItem | |
key={product.id} | |
product={product} | |
onAddToCartClicked={() => addToCart(product.id)} /> | |
)} | |
</ProductsList> | |
) | |
} | |
ProductsContainer.propTypes = { | |
products: PropTypes.arrayOf(PropTypes.shape({ | |
id: PropTypes.number.isRequired, | |
title: PropTypes.string.isRequired, | |
price: PropTypes.number.isRequired, | |
inventory: PropTypes.number.isRequired | |
})).isRequired, | |
addToCart: PropTypes.func.isRequired | |
} | |
const mapStateToProps = state => ({ | |
products: getVisibleProducts(state.products) | |
}) | |
export default connect( | |
mapStateToProps, | |
{ addToCart } | |
)(ProductsContainer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment