Recipes
The goal of this page is to have some of my favorite recipes in React that can be copy-pasted as a good baseline.
Context
import { createContext, type ReactNode, useContext } from 'react';
type ContextValue = any;
const YourContext = createContext<ContextValue | null>(null);
export const useYourContext = () => {
const context = useContext(YourContext);
if (!context) {
throw new Error('Cannot use YourContext outside of a YourContextProvider');
}
return context;
};
type YourContextProviderProps = {
children: ReactNode;
value: ContextValue;
};
export const YourContextProvider = ({
children,
value,
}: YourContextProviderProps) => {
return <YourContext value={value}>{children}</YourContext>;
};
-
Simplified Context Consumption
- Encapsulation with a custom hook eliminates the need to directly import and use
useContext
. - Developers can simply call
useYourContext()
instead ofuseContext(YourContext)
.
- Encapsulation with a custom hook eliminates the need to directly import and use
-
Error Handling Built-In
- The custom hook includes a guard to ensure the context is used within its provider.
- Prevents runtime errors caused by accessing a context outside its intended scope.
-
Co-location of Provider and Hook
- Both
YourContextProvider
anduseYourContext
are defined in the same module. - Simplifies setup and ensures consistency across the codebase.
- Both
-
Cleaner Component Code
- Components using the context don’t need to import both the
YourContext
anduseContext
. - Reduces coupling and improves maintainability.
- Components using the context don’t need to import both the
-
Flexible and Reusable
- The pattern supports any type of context value, from simple data to complex objects or functions.
- Can be adapted to a wide variety of use cases.
-
React 19-Ready
- Simplifies provider setup by using React 19’s
<Context value={}>
syntax instead of.Provider
.
- Simplifies provider setup by using React 19’s
-
Improved Readability
- Clearly defines where the context is provided (
YourContextProvider
) and how it’s consumed (useYourContext
). - Makes it easier to reason about data flow in the component tree.
- Clearly defines where the context is provided (
-
Developer Convenience
- The hook provides easy access to the context without requiring additional imports or boilerplate.
- The inclusion of the provider makes the API intuitive and self-contained.