Context provides a way to pass data through the component tree without having to pass props down manually at every level.
import { createContext } from "react";
export const CountContext = createContext(0);
or
export const CountContext = createContext({
count: 0,
setCount: () => {},
});
<CountContext.Provider value={count}>
<Count count={count} setCount={setCount} />
</CountContext.Provider>
or
<CountContext.Provider value={{ count, setCount }}>
<Count />
</CountContext.Provider>
const count = useContext(CountContext);
return <div>{count}</div>;
const { count, setCount } = useContext(CountContext);