Home / Learn React / React Cheat Sheet
React Cheat Sheet
One page with the syntax you use every day. Print it or save it as a PDF.
In shortKeep this next to you while you code.
One page of what you use most. Press the button, then choose "Save as PDF" in the print window.
Basics
function App() { return <div/>; } | Component |
<Card title="x" /> | Props |
{expr} | JavaScript in JSX |
className, htmlFor | JSX attribute names |
<>...</> | Fragment |
{ok && <p/>} | Conditional render |
{ok ? <a/> : <b/>} | Either / or |
{list.map(x => <li key={x.id}/>)} | List with keys |
State and events
const [v, setV] = useState(0) | State |
setV(v => v + 1) | Updater form |
setObj({ ...obj, a: 1 }) | Copy before change |
onClick={() => go(id)} | Event handler |
onChange={e => set(e.target.value)} | Controlled input |
e.preventDefault() | Stop form reload |
Hooks
useEffect(fn, [dep]) | Side effect |
return () => cleanup | Effect cleanup |
useRef(null) | DOM node or mutable box |
useMemo(() => v, [dep]) | Cache a value |
useCallback(fn, [dep]) | Cache a function |
useReducer(reducer, init) | Complex state |
useContext(Ctx) | Read context |
useTransition() | Mark updates low priority |
useDeferredValue(v) | Lagging copy of a value |
useId() | Unique id for labels |
React 19
<form action={fn}> | Form Action |
useActionState(fn, init) | [state, action, pending] |
useFormStatus() | Pending of parent form |
useOptimistic(v, fn) | Instant UI |
use(promise) | Read a promise |
<Ctx value={x}> | Context as provider |
ref as a prop | No forwardRef needed |
<Activity mode="hidden"> | Hide, keep state |
Built-ins and DOM
<Suspense fallback={}> | Loading boundary |
lazy(() => import(...)) | Code splitting |
createPortal(el, node) | Render elsewhere |
<StrictMode> | Dev checks |
<Profiler onRender> | Measure |
memo(Component) | Skip same props |
Project commands
npm create vite@latest | New app |
npm run dev | Dev server |
npm run build | Create dist/ |
npm install pkg | Add a library |
npm test | Run tests |
SQL
CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, ...) | New table |
INSERT INTO t (a) VALUES ('x') | Add a row |
SELECT a, b FROM t WHERE c = 1 | Read rows |
ORDER BY id DESC LIMIT 10 OFFSET 20 | Sort and page |
WHERE title LIKE '%sql%' | Contains |
UPDATE t SET a = 1 WHERE id = 5 | Change |
DELETE FROM t WHERE id = 5 | Remove |
SELECT COUNT(*) FROM t | Count rows |
JOIN u ON u.id = t.user_id | Combine tables |
CREATE INDEX i ON t (col) | Speed up search |
PHP PDO and fetch
$st = $pdo->prepare("... :x"); $st->execute([":x" => $v]); | Safe query |
$st->fetchAll() | All rows |
$pdo->lastInsertId() | New id |
password_hash($p, PASSWORD_DEFAULT) | Hash |
password_verify($p, $hash) | Check |
fetch(url, { method, headers, body: JSON.stringify(d), credentials: "same-origin" }) | Call the API |
const json = await res.json() | Read JSON |
encodeURIComponent(q) | Safe URL value |