Created
October 26, 2024 19:08
-
-
Save Vishalll07/41d34ffe9fedddb4cc67b4134752a97d to your computer and use it in GitHub Desktop.
useContext and about ContextAPI
This file contains hidden or 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, useState, createContext } from 'react'; | |
// Create a context for the café (the "bulletin board") | |
const cafeContext = createContext( ); | |
const cafeProvider = ({ children }) => { | |
// State shared by the entire café | |
const [cafeStatus , setCafeStatus] = useState('Open'); | |
return { | |
<cafeContext.Provider value={{ cafeStatus, setCafeStatus }}> | |
{children} | |
</CafeContext.Provider> | |
); | |
const WaiterSection = () => { | |
const { cafeStatus } = useContext(CafeContext); | |
return (... ); | |
}; | |
//Component for the kitchen | |
const KitchenSection = () => { | |
const { cafeStatus, setCafeStatus } = useContext(CafeContext); | |
return (...); | |
}; | |
const CafeContext = () => { | |
return ( | |
<cafeProvider> | |
<WaiterSection /> | |
<KitchenSection /> | |
</CafeProvider> | |
); | |
}; | |
export default CafeWithContext; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment