



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




function Greeting({ name, children }) { return (
Hello, {name}! {children}
); }
// Usage
Welcome
{isLoggedIn? : } {error &&
{error}
}{items.map(item => ( {item.title} ))}
useState
const [count, setCount] = useState(0);
setCount(count + 1); // direct setCount(prev => prev + 1); // when based on previous state
// Objects/arrays: never mutate, create copies setUser(u => ({ ...u, name: 'Ada' })); setTodos(t => [...t, newTodo]); setTodos(t => t.filter(x => x.id !== id));
useEffect
useEffect(() => { // runs after render const id = setInterval(tick, 1000); return () => clearInterval(id); // cleanup }, [dependency]);
Events & Forms
function Form() { const [email, setEmail] = useState('');
function handleSubmit(e) { e.preventDefault(); console.log(email); }
return (
setEmail(e.target.value)} /> Send
); }
Custom Hooks
function useLocalStorage(key, initial) { const [value, setValue] = useState(() => { const saved = localStorage.getItem(key); return saved? JSON.parse(saved) : initial; }); useEffect(() => { localStorage.setItem(key, JSON.stringify(value)); }, [key, value]); return [value, setValue]; }
Data Fetching Pattern
useEffect(() => { const controller = new AbortController(); fetch('/api/items', { signal: controller.signal }) .then(r => r.json()) .then(setItems) .catch(err => err.name !== 'AbortError' && setError(err)); return () => controller.abort(); }, []);
Performance
const Chart = lazy(() => import('./Chart')); }>
React 19 Highlights
// use(): read a promise or context during render const data = use(dataPromise);
// useActionState: form actions with pending state const [state, action, pending] = useActionState(saveFn, initialState); ...
// useOptimistic: instant UI while a mutation runs const [optimisticItems, addOptimistic] = useOptimistic(items);