Skip to content

Instantly share code, notes, and snippets.

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 highlander08/0da673aea8d97eb08c2cdc94a0f7b82a to your computer and use it in GitHub Desktop.
Save highlander08/0da673aea8d97eb08c2cdc94a0f7b82a to your computer and use it in GitHub Desktop.
Providing Context and Local Storage Using Hooks
// Providing Context
// ==================
import React, {useState, useEffect} from "react"
const Context = React.createContext()
function ContextProvider({children}) {
const [allPhotos, setAllPhotos] = useState([])
const [cartItems, setCartItems] = useState([])
// Local Storage: setting & getting data
useEffect(() => {
const cartItemsData = JSON.parse(localStorage.getItem('cartItems'))
if (cartItemsData) {
setCartItems(cartItemsData)
}
}, [])
useEffect(() => {
localStorage.setItem('cartItems', JSON.stringify(cartItems))
}, [cartItems])
const url = "https://raw.githubusercontent.com/bobziroll/scrimba-react-bootcamp-images/master/images.json"
useEffect(() => {
fetch(url)
.then(res => res.json())
.then(data => setAllPhotos(data))
}, [])
function toggleFavorite(id) {
const updatedArr = allPhotos.map(photo => {
if(photo.id === id) {
return {...photo, isFavorite: !photo.isFavorite}
}
return photo
})
setAllPhotos(updatedArr)
}
function addToCart(newItem) {
setCartItems(prevItems => [...prevItems, newItem])
}
function removeFromCart(id) {
setCartItems(prevItems => prevItems.filter(item => item.id !== id))
}
return (
<Context.Provider value={{allPhotos, toggleFavorite, cartItems, addToCart, removeFromCart}}>
{children}
</Context.Provider>
)
}
export {ContextProvider, Context}
// Retrieving state using useContext
// =================================
import React, {useState, useContext} from "react"
import PropTypes from "prop-types"
import {Context} from "../Context"
function Image({className, img}) {
const [hovered, setHovered] = useState(false)
const {toggleFavorite, addToCart, cartItems, removeFromCart} = useContext(Context)
function heartIcon() {
if(img.isFavorite) {
return <i className="ri-heart-fill favorite" onClick={() => toggleFavorite(img.id)}></i>
} else if(hovered) {
return <i className="ri-heart-line favorite" onClick={() => toggleFavorite(img.id)}></i>
}
}
function cartIcon() {
const alreadyInCart = cartItems.some(item => item.id === img.id)
if(alreadyInCart) {
return <i className="ri-shopping-cart-fill cart" onClick={() => removeFromCart(img.id)}></i>
} else if(hovered) {
return <i className="ri-add-circle-line cart" onClick={() => addToCart(img)}></i>
}
}
return (
<div
className={`${className} image-container`}
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
<img src={img.url} className="image-grid"/>
{heartIcon()}
{cartIcon()}
</div>
)
}
Image.propTypes = {
className: PropTypes.string,
img: PropTypes.shape({
id: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
isFavorite: PropTypes.bool
})
}
export default Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment