Migrating Products Data to Context API from Redux
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,{createContext, useState, useEffect} from 'react' | |
import shop from '../api/shop' | |
export const ProductsContext = createContext({ | |
test: '', | |
products: [] | |
}) | |
const ProductsProvider = ({children}) => { | |
const [test] = useState('the products provider has been successfully connected :)') | |
const [products, setProducts] = useState([]) | |
useEffect(async ()=> { | |
const response = await shop.getProducts(products => products) | |
setProducts(response) | |
},[]) | |
return ( | |
<ProductsContext.Provider value={{test, products}}> | |
{children} | |
</ProductsContext.Provider> | |
) | |
} | |
export default ProductsProvider |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment