Skip to content

Instantly share code, notes, and snippets.

@01Clarian
Created December 10, 2019 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 01Clarian/c177f77ba7f03cf40ab998afd570ab68 to your computer and use it in GitHub Desktop.
Save 01Clarian/c177f77ba7f03cf40ab998afd570ab68 to your computer and use it in GitHub Desktop.
Migrating Products Data to Context API from Redux
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