typescript
74 lines · 1 tab
Maya Patel
Jan 2026
1 tab
import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react'
import { User } from '@/types'
import api from '@/services/api'
interface AuthContextType {
user: User | null
loading: boolean
login: (email: string, password: string) => Promise<void>
logout: () => void
isAuthenticated: boolean
}
const AuthContext = createContext<AuthContextType | undefined>(undefined)
export function AuthProvider({ children }: { children: ReactNode }) {
const [user, setUser] = useState<User | null>(null)
const [loading, setLoading] = useState(true)
useEffect(() => {
const token = localStorage.getItem('authToken')
if (token) {
fetchCurrentUser()
} else {
setLoading(false)
}
}, [])
const fetchCurrentUser = async () => {
try {
const { data } = await api.get<User>('/auth/me')
setUser(data)
} catch (error) {
localStorage.removeItem('authToken')
} finally {
setLoading(false)
}
}
const login = async (email: string, password: string) => {
const { data } = await api.post<{ user: User; token: string }>('/auth/login', {
email,
password,
})
localStorage.setItem('authToken', data.token)
setUser(data.user)
}
const logout = () => {
localStorage.removeItem('authToken')
setUser(null)
}
return (
<AuthContext.Provider
value={{
user,
loading,
login,
logout,
isAuthenticated: !!user,
}}
>
{children}
</AuthContext.Provider>
)
}
export function useAuth() {
const context = useContext(AuthContext)
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider')
}
return context
}
1 file · typescript
Explain with highlit
While React Query handles server state, I use Context API for client-side UI state like theme, sidebar visibility, or current user. Each context lives in its own file with a custom hook for consuming it. The context provider wraps the app at a high level and provides both state and updater functions. I avoid putting too much in context—only truly global state belongs here. For component-local state that's shared with a few children, prop drilling or composition is clearer. The useReducer hook works well for complex state transitions within a context. This pattern gives centralized state management without Redux's boilerplate for simple use cases.
Related snips
typescript
import axios, { AxiosError } from 'axios'
import { v4 as uuidv4 } from 'uuid'
const api = axios.create({
baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3000/api/v1',
timeout: 15000,
Axios API client with interceptors
react
axios
api
by Maya Patel
1 tab
typescript
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
interface FilterState {
search: string
category: string | null
Zustand for lightweight state management
react
zustand
state-management
by Maya Patel
2 tabs
swift
import SwiftUI
struct ContentView: View {
@State private var username = ""
@State private var isLoggedIn = false
@StateObject private var viewModel = LoginViewModel()
SwiftUI declarative UI with state management
swift
swiftui
ios
by Sofia Martinez
2 tabs
typescript
import React from "react";
type FallbackProps = {
error: Error;
reset: () => void;
};
React Error Boundary + error reporting hook
react
frontend
error-boundary
by codesnips
3 tabs
javascript
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
openAnalyzer: true,
});
/** @type {import('next').NextConfig} */
Next.js bundle analyzer for targeted performance work
nextjs
performance
tooling
by codesnips
4 tabs
typescript
import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '@/contexts/AuthContext'
interface ProtectedRouteProps {
children: React.ReactNode
}
React Router with protected routes
react
react-router
routing
by Maya Patel
2 tabs
Share this code
Here's the card — post it anywhere.