React.createContext

Creates a Context object. Components can subscribe to this context to receive its current value without prop drilling.

Syntax

react
const MyContext = React.createContext(defaultValue)

Example

react
const AuthContext = React.createContext(null);

function App() {
  const [user, setUser] = useState(null);
  return (
    <AuthContext.Provider value={{ user, setUser }}>
      <Router />
    </AuthContext.Provider>
  );
}