Docsity

Complete React Cheat Sheet, Cheat Sheet of Programming Languages

Toggle sidebar
bg1
React Cheat Sheet
Quick reference for modern React (18/19) with hooks and function components.
Components & JSX
function Greeting({ name, children }) {
return (
<div className="card">
<h1>Hello, {name}!</h1>
{children}
</div>
);
}
// Usage
<Greeting name="Ada"><p>Welcome</p></Greeting>
Components start with a capital letter; return a single root (or a fragment <>...</> ).
Use className , htmlFor , and camelCase event names ( onClick , onChange ).
Expressions go in {} ; styles take objects: style={{ color: 'red' }} .
Conditional Rendering & Lists
{isLoggedIn ? <Dashboard /> : <Login />}
{error && <p className="error">{error}</p>}
<ul>
{items.map(item => (
<li key={item.id}>{item.title}</li>
))}
</ul>
Always give list items a stable, unique key (not the array index if the list can reorder).
React Cheat Sheet
Page 1 of 6
pf3
pf4
pf5

Partial preview of the text

Download Complete React Cheat Sheet and more Cheat Sheet Programming Languages in PDF only on Docsity!

React Cheat Sheet

Quick reference for modern React (18/19) with hooks and function components.

Components & JSX

function Greeting({ name, children }) { return (

Hello, {name}! {children}

); }

// Usage

Welcome

Components start with a capital letter; return a single root (or a fragment <>...</> ).

Use className , htmlFor , and camelCase event names ( onClick , onChange ).

Expressions go in {} ; styles take objects: style={{ color: 'red' }}.

Conditional Rendering & Lists

{isLoggedIn? : } {error &&

{error}

}

{items.map(item => ( {item.title} ))}

Always give list items a stable, unique key (not the array index if the list can reorder).

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]);

Tip: don't use effects for things you can compute during render or handle in an event

handler.

Dependency array Runs

omitted after every render

[] once on mount

[a, b] on mount and when^ a or^ b change

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]; }

Custom hooks must start with use and can call other hooks.

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(); }, []);

For real apps, prefer TanStack Query or SWR for caching, retries, and loading states.

Performance

React.memo(Component) skips re-render if props are unchanged.

lazy +^ Suspense for code splitting:

const Chart = lazy(() => import('./Chart')); }>

Keep state as local as possible; lift it up only when siblings need it.

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);

ref can now be passed as a regular prop to function components (no forwardRef

needed).