React Context Types in TypeScript
React Context Types
Section titled “React Context Types”Basic Context
Section titled “Basic Context”interface ThemeContextType { theme: 'light' | 'dark'; toggleTheme: () => void;}
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
function useTheme(): ThemeContextType { const context = useContext(ThemeContext); if (!context) throw new Error('useTheme must be used within ThemeProvider'); return context;}Complex Context with Reducer
Section titled “Complex Context with Reducer”interface AuthState { user: User | null; isLoading: boolean; error: string | null;}
type AuthAction = | { type: 'LOGIN_START' } | { type: 'LOGIN_SUCCESS'; payload: User } | { type: 'LOGIN_FAILURE'; payload: string } | { type: 'LOGOUT' };
interface AuthContextType extends AuthState { dispatch: React.Dispatch<AuthAction>; login: (email: string, password: string) => Promise<void>; logout: () => void;}Interview Questions
Section titled “Interview Questions”Easy: How do you type createContext?
Medium: Why is Context typically initialized with undefined?
Hard: How would you type a context that uses useReducer?