# Zaawansowane React Hooks: wzorce i optymalizacje > Opanowanie zaawansowanych React Hooks ze sprawdzonymi wzorcami. Custom hooks, zoptymalizowany useEffect, useMemo, useCallback i techniki wydajności. - Published: 2026-02-16 - Updated: 2026-04-28 - Author: SharpSkill - Tags: react hooks, useEffect, custom hooks, performance, react patterns - Reading time: 12 min --- React Hooks zrewolucjonizowały sposób zarządzania stanem i efektami ubocznymi w komponentach funkcyjnych. Poza podstawowym użyciem `useState` i `useEffect`, zaawansowane wzorce pozwalają tworzyć kod wielokrotnego użytku, wydajny i łatwy w utrzymaniu. > **Wymagania wstępne** > > Niniejszy przewodnik zakłada znajomość podstawowych Hooks (useState, useEffect, useContext). Przykłady wykorzystują React 18+ oraz TypeScript dla solidnego typowania. ## Opanowanie useEffect: unikanie typowych pułapek Hook `useEffect` bywa często niewłaściwie rozumiany i wykorzystywany. Dogłębne zrozumienie jego działania pozwala uniknąć subtelnych błędów oraz problemów z wydajnością. Fundamentalna zasada: `useEffect` synchronizuje komponent z zewnętrznym systemem. Jeśli efekt nie komunikuje się ze światem zewnętrznym (API, DOM, timer), prawdopodobnie nie jest potrzebny. ```tsx // hooks/useDocumentTitle.ts // Custom hook to synchronize document title import { useEffect } from 'react' export function useDocumentTitle(title: string) { useEffect(() => { // Save the previous title const previousTitle = document.title // Update the title document.title = title // Cleanup: restore previous title on unmount return () => { document.title = previousTitle } }, [title]) // Only trigger when title changes } ``` Najczęstszym błędem jest pominięcie tablicy zależności lub uwzględnienie wartości niestabilnych. Każda wartość używana w efekcie musi znaleźć się w zależnościach. ```tsx // components/UserProfile.tsx 'use client' import { useEffect, useState } from 'react' interface User { id: string name: string email: string } interface UserProfileProps { userId: string } export function UserProfile({ userId }: UserProfileProps) { const [user, setUser] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { // Flag to prevent state updates after unmount let isMounted = true // Controller to cancel pending requests const controller = new AbortController() async function fetchUser() { setLoading(true) setError(null) try { const response = await fetch(`/api/users/${userId}`, { signal: controller.signal }) if (!response.ok) { throw new Error('User not found') } const data = await response.json() // Check if component is still mounted if (isMounted) { setUser(data) } } catch (err) { // Ignore abort errors if (err instanceof Error && err.name !== 'AbortError') { if (isMounted) { setError(err.message) } } } finally { if (isMounted) { setLoading(false) } } } fetchUser() // Cleanup: abort request and mark as unmounted return () => { isMounted = false controller.abort() } }, [userId]) // Re-run effect when userId changes if (loading) return
Loading...
if (error) return
Error: {error}
if (!user) return null return (

{user.name}

{user.email}

) } ``` Ten wzorzec wykorzystujący `AbortController` oraz flagę `isMounted` zapobiega wyciekom pamięci i aktualizacjom stanu w odmontowanych komponentach. ## Tworzenie custom hooks wielokrotnego użytku Custom hooks enkapsulują logikę wielokrotnego użytku. Dobry custom hook respektuje zasadę pojedynczej odpowiedzialności i udostępnia czyste API. ```tsx // hooks/useLocalStorage.ts // Generic hook to persist state in localStorage import { useState, useEffect, useCallback } from 'react' export function useLocalStorage( key: string, initialValue: T ): [T, (value: T | ((prev: T) => T)) => void, () => void] { // Initialize state with stored value or default const [storedValue, setStoredValue] = useState(() => { if (typeof window === 'undefined') { return initialValue } try { const item = window.localStorage.getItem(key) return item ? JSON.parse(item) : initialValue } catch (error) { console.warn(`Error reading localStorage "${key}":`, error) return initialValue } }) // Sync with localStorage on every change useEffect(() => { if (typeof window === 'undefined') return try { window.localStorage.setItem(key, JSON.stringify(storedValue)) } catch (error) { console.warn(`Error writing localStorage "${key}":`, error) } }, [key, storedValue]) // Function to update value const setValue = useCallback((value: T | ((prev: T) => T)) => { setStoredValue((prev) => { const nextValue = value instanceof Function ? value(prev) : value return nextValue }) }, []) // Function to remove value const removeValue = useCallback(() => { setStoredValue(initialValue) if (typeof window !== 'undefined') { window.localStorage.removeItem(key) } }, [key, initialValue]) return [storedValue, setValue, removeValue] } ``` Ten hook można wykorzystać w dowolnym komponencie, aby utrwalać dane. ```tsx // components/ThemeToggle.tsx 'use client' import { useLocalStorage } from '@/hooks/useLocalStorage' type Theme = 'light' | 'dark' | 'system' export function ThemeToggle() { const [theme, setTheme] = useLocalStorage('theme', 'system') return ( ) } ``` > **Konwencja nazewnicza** > > Custom hooks zawsze zaczynają się od "use" (useLocalStorage, useFetch, useDebounce). Konwencja ta umożliwia React weryfikację reguł hooków oraz poprawne działanie narzędzi linterów. ## Wzorzec kompozycji z useReducer Dla złożonego stanu z wieloma możliwymi akcjami `useReducer` oferuje bardziej przewidywalną strukturę niż wielokrotne wywołania `useState`. ```tsx // hooks/useCart.ts // Cart management hook with useReducer import { useReducer, useCallback, useMemo } from 'react' interface CartItem { id: string name: string price: number quantity: number } interface CartState { items: CartItem[] isOpen: boolean } type CartAction = | { type: 'ADD_ITEM'; payload: Omit } | { type: 'REMOVE_ITEM'; payload: string } | { type: 'UPDATE_QUANTITY'; payload: { id: string; quantity: number } } | { type: 'CLEAR_CART' } | { type: 'TOGGLE_CART' } function cartReducer(state: CartState, action: CartAction): CartState { switch (action.type) { case 'ADD_ITEM': { const existingItem = state.items.find( (item) => item.id === action.payload.id ) if (existingItem) { // Increment quantity if item exists return { ...state, items: state.items.map((item) => item.id === action.payload.id ? { ...item, quantity: item.quantity + 1 } : item ) } } // Add new item return { ...state, items: [...state.items, { ...action.payload, quantity: 1 }] } } case 'REMOVE_ITEM': return { ...state, items: state.items.filter((item) => item.id !== action.payload) } case 'UPDATE_QUANTITY': return { ...state, items: state.items.map((item) => item.id === action.payload.id ? { ...item, quantity: action.payload.quantity } : item ).filter((item) => item.quantity > 0) } case 'CLEAR_CART': return { ...state, items: [] } case 'TOGGLE_CART': return { ...state, isOpen: !state.isOpen } default: return state } } const initialState: CartState = { items: [], isOpen: false } export function useCart() { const [state, dispatch] = useReducer(cartReducer, initialState) // Memoized actions to avoid re-renders const addItem = useCallback( (item: Omit) => { dispatch({ type: 'ADD_ITEM', payload: item }) }, [] ) const removeItem = useCallback((id: string) => { dispatch({ type: 'REMOVE_ITEM', payload: id }) }, []) const updateQuantity = useCallback((id: string, quantity: number) => { dispatch({ type: 'UPDATE_QUANTITY', payload: { id, quantity } }) }, []) const clearCart = useCallback(() => { dispatch({ type: 'CLEAR_CART' }) }, []) const toggleCart = useCallback(() => { dispatch({ type: 'TOGGLE_CART' }) }, []) // Memoized derived values const total = useMemo( () => state.items.reduce( (sum, item) => sum + item.price * item.quantity, 0 ), [state.items] ) const itemCount = useMemo( () => state.items.reduce((sum, item) => sum + item.quantity, 0), [state.items] ) return { items: state.items, isOpen: state.isOpen, total, itemCount, addItem, removeItem, updateQuantity, clearCart, toggleCart } } ``` Takie podejście centralizuje całą logikę koszyka i ułatwia testy jednostkowe. ## Optymalizacja z useMemo i useCallback Te hooki zapobiegają zbędnym obliczeniom i ponownemu tworzeniu funkcji. Ich stosowanie powinno być punktowe: wszechobecne ich użycie pogarsza wydajność zamiast ją poprawiać. ```tsx // components/ProductList.tsx 'use client' import { useState, useMemo, useCallback, memo } from 'react' interface Product { id: string name: string price: number category: string inStock: boolean } interface ProductListProps { products: Product[] } // Memoized child component const ProductCard = memo(function ProductCard({ product, onAddToCart }: { product: Product onAddToCart: (id: string) => void }) { console.log(`Render ProductCard: ${product.name}`) return (

{product.name}

${product.price}

) }) export function ProductList({ products }: ProductListProps) { const [filter, setFilter] = useState('') const [sortBy, setSortBy] = useState<'name' | 'price'>('name') const [cart, setCart] = useState([]) // useMemo: memoize result of expensive computation const filteredAndSortedProducts = useMemo(() => { console.log('Computing filteredAndSortedProducts') let result = products // Filter by name if (filter) { result = result.filter((p) => p.name.toLowerCase().includes(filter.toLowerCase()) ) } // Sort result = [...result].sort((a, b) => { if (sortBy === 'name') { return a.name.localeCompare(b.name) } return a.price - b.price }) return result }, [products, filter, sortBy]) // Recompute only when these deps change // useCallback: memoize a function const handleAddToCart = useCallback((productId: string) => { setCart((prev) => [...prev, productId]) }, []) // Empty deps: function never changes // Memoized statistics const stats = useMemo( () => ({ total: filteredAndSortedProducts.length, inStock: filteredAndSortedProducts.filter((p) => p.inStock).length, avgPrice: filteredAndSortedProducts.reduce((sum, p) => sum + p.price, 0) / filteredAndSortedProducts.length || 0 }), [filteredAndSortedProducts] ) return (
setFilter(e.target.value)} placeholder="Search..." className="px-3 py-2 border rounded-lg" />

{stats.total} products ({stats.inStock} in stock) - Average price: ${stats.avgPrice.toFixed(2)}

{filteredAndSortedProducts.map((product) => ( ))}
) } ``` > **Kiedy używać useMemo/useCallback?** > > Te hooki dodają złożoności. Warto zarezerwować je dla: (1) naprawdę kosztownych obliczeń, (2) propsów przekazywanych do komponentów memoizowanych przez `memo()`, (3) zależności innych hooków. ## Hook debounce dla pól wyszukiwania Debouncing zapobiega zbyt częstemu wykonywaniu akcji, typowo podczas wpisywania w pole wyszukiwania. ```tsx // hooks/useDebounce.ts // Generic debounce hook import { useState, useEffect } from 'react' export function useDebounce(value: T, delay: number): T { const [debouncedValue, setDebouncedValue] = useState(value) useEffect(() => { // Create a timer that updates the value after delay const timer = setTimeout(() => { setDebouncedValue(value) }, delay) // Cleanup: cancel timer if value changes before delay return () => { clearTimeout(timer) } }, [value, delay]) return debouncedValue } ``` ```tsx // components/SearchInput.tsx 'use client' import { useState, useEffect } from 'react' import { useDebounce } from '@/hooks/useDebounce' interface SearchResult { id: string title: string } export function SearchInput() { const [query, setQuery] = useState('') const [results, setResults] = useState([]) const [loading, setLoading] = useState(false) // Debounce the query by 300ms const debouncedQuery = useDebounce(query, 300) useEffect(() => { // Don't search if query is empty if (!debouncedQuery.trim()) { setResults([]) return } async function search() { setLoading(true) try { const response = await fetch( `/api/search?q=${encodeURIComponent(debouncedQuery)}` ) const data = await response.json() setResults(data.results) } catch (error) { console.error('Search error:', error) } finally { setLoading(false) } } search() }, [debouncedQuery]) // Only triggered when debouncedQuery changes return (
setQuery(e.target.value)} placeholder="Search..." className="w-full px-4 py-2 border rounded-lg" /> {loading && (
)} {results.length > 0 && (
    {results.map((result) => (
  • {result.title}
  • ))}
)}
) } ``` Komponent uruchamia wyszukiwanie dopiero 300 ms po zaprzestaniu pisania, oszczędzając dziesiątki niepotrzebnych żądań. ## useImperativeHandle dla zaawansowanych refów Ten hook udostępnia komponentowi nadrzędnemu wybrane metody komponentu potomnego za pośrednictwem refa. ```tsx // components/VideoPlayer.tsx 'use client' import { useRef, useImperativeHandle, forwardRef, useState, useCallback } from 'react' // Interface of exposed methods export interface VideoPlayerRef { play: () => void pause: () => void seek: (time: number) => void getCurrentTime: () => number } interface VideoPlayerProps { src: string poster?: string } export const VideoPlayer = forwardRef( function VideoPlayer({ src, poster }, ref) { const videoRef = useRef(null) const [isPlaying, setIsPlaying] = useState(false) // Expose only desired methods to parent useImperativeHandle( ref, () => ({ play: () => { videoRef.current?.play() setIsPlaying(true) }, pause: () => { videoRef.current?.pause() setIsPlaying(false) }, seek: (time: number) => { if (videoRef.current) { videoRef.current.currentTime = time } }, getCurrentTime: () => { return videoRef.current?.currentTime ?? 0 } }), [] // No deps: methods always use current ref ) const handlePlayPause = useCallback(() => { if (isPlaying) { videoRef.current?.pause() } else { videoRef.current?.play() } setIsPlaying(!isPlaying) }, [isPlaying]) return (
) } ) ``` ```tsx // components/VideoController.tsx 'use client' import { useRef } from 'react' import { VideoPlayer, VideoPlayerRef } from './VideoPlayer' export function VideoController() { const playerRef = useRef(null) const handleSkipForward = () => { if (playerRef.current) { const currentTime = playerRef.current.getCurrentTime() playerRef.current.seek(currentTime + 10) } } return (
) } ``` Ten wzorzec szczególnie sprawdza się przy komponentach multimedialnych, formularzach lub każdym interfejsie wymagającym imperatywnej kontroli. ## Hook do zarządzania cyklem życia żądań Zaawansowany hook do obsługi stanów ładowania, błędu i danych ze ścisłym typowaniem. ```tsx // hooks/useFetch.ts // Generic hook for HTTP requests import { useState, useEffect, useCallback, useRef } from 'react' interface UseFetchState { data: T | null loading: boolean error: Error | null } interface UseFetchOptions { immediate?: boolean // Execute immediately on mount onSuccess?: (data: T) => void onError?: (error: Error) => void } export function useFetch( url: string, options: UseFetchOptions = {} ) { const { immediate = true, onSuccess, onError } = options const [state, setState] = useState>({ data: null, loading: immediate, error: null }) // Ref to prevent updates after unmount const mountedRef = useRef(true) // Ref for abort controller const abortControllerRef = useRef(null) const execute = useCallback(async () => { // Abort previous request if in progress abortControllerRef.current?.abort() abortControllerRef.current = new AbortController() setState((prev) => ({ ...prev, loading: true, error: null })) try { const response = await fetch(url, { signal: abortControllerRef.current.signal }) if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`) } const data: T = await response.json() if (mountedRef.current) { setState({ data, loading: false, error: null }) onSuccess?.(data) } return data } catch (error) { // Ignore abort errors if (error instanceof Error && error.name === 'AbortError') { return null } const err = error instanceof Error ? error : new Error('Unknown error') if (mountedRef.current) { setState({ data: null, loading: false, error: err }) onError?.(err) } return null } }, [url, onSuccess, onError]) const reset = useCallback(() => { setState({ data: null, loading: false, error: null }) }, []) // Execute on mount if immediate=true useEffect(() => { if (immediate) { execute() } return () => { mountedRef.current = false abortControllerRef.current?.abort() } }, [execute, immediate]) return { ...state, execute, reset, isIdle: !state.loading && !state.data && !state.error } } ``` Ten hook udostępnia kompletne API do obsługi dowolnego żądania z automatycznym anulowaniem oraz zarządzaniem cyklem życia. ## Podsumowanie Zaawansowane wzorce React Hooks pozwalają tworzyć kod łatwy w utrzymaniu i wydajny. Najważniejsze punkty: - ✅ **useEffect**: zawsze uwzględniać cleanup oraz obsługę race conditions z AbortController - ✅ **Custom hooks**: enkapsulować wielokrotnie używaną logikę z czystym API - ✅ **useReducer**: preferowany dla złożonego stanu z wieloma akcjami - ✅ **useMemo/useCallback**: stosować punktowo, tylko gdy konieczne - ✅ **useDebounce**: ograniczać częste wywołania w wyszukiwarkach - ✅ **useImperativeHandle**: udostępniać metody imperatywne w kontrolowany sposób Opanowanie tych wzorców odróżnia średniozaawansowanych deweloperów React od ekspertów. Każdy hook rozwiązuje konkretny problem: dobranie właściwego narzędzia we właściwym momencie jest kluczem do solidnej architektury React. --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/pl/blog/react-next/advanced-react-hooks-patterns-optimizations