createContext
Create a new context. The default value is optional.
createContext(defaultValue?): Context<T>
Create a new context. The default value is optional.
const userContext = createContext();
// the value to provide
const user = {
id: 1,
name: 'John Doe',
};
// provide the context value to the receiver
context.provide(user, handler);
function handler() {
// get the context value
const { id, name } = useContext(context);
console.log(id, name); // 1, John Doe
}
Parameter | Type | Optional | Description |
---|---|---|---|
defaultValue | T | ✅ | The default value of the context |