Skip to content

Instantly share code, notes, and snippets.

@Vishalll07
Created October 26, 2024 19:08
Show Gist options
  • Save Vishalll07/41d34ffe9fedddb4cc67b4134752a97d to your computer and use it in GitHub Desktop.
Save Vishalll07/41d34ffe9fedddb4cc67b4134752a97d to your computer and use it in GitHub Desktop.
useContext and about ContextAPI
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