Skip to content

Instantly share code, notes, and snippets.

View Vishalll07's full-sized avatar
🎯
Focusing

VISHAL SAHOO Vishalll07

🎯
Focusing
View GitHub Profile
@Vishalll07
Vishalll07 / useReducer.js
Created October 26, 2024 19:11
Advance State Management using useReducer()
import React, { useReducer } from 'react';
const cafeReducer = (state , action) => {
switch(action.type) {
case 'ADD_ORDER' :
return { ...state, orders: state.orders + 1 } ;
case 'REMOVE_ORDER' :
return { ...state, orders: state.orders - 1 } ;
default:
return state;
}
@Vishalll07
Vishalll07 / contextApi.js
Created October 26, 2024 19:08
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}