Skip to content

React Cheat Sheet

Last verified May 2026 — runs in your browser
React Cheatsheet
function App() {
  return <h1>Hello</h1>;
}

Function component

Components
const App = () => <h1>Hello</h1>;

Arrow function component

Components
function Greet({ name }) {
  return <p>Hi {name}</p>;
}

Component with props

Components
export default App;

Export component as default

Components
<App title="Hi" count={5} />

Use component with props

Components
<>
  <A /> <B />
</>

Fragment (no wrapper)

Components
{items.map(item => <li key={item.id}>{item.name}</li>)}

Render list with keys

Components
{condition && <Alert />}

Conditional render (and)

Components
{loading ? <Spinner /> : <Data />}

Conditional render (ternary)

Components
<div className="btn primary">Click</div>

CSS class (className)

Components
<div style={{color: 'red', fontSize: 14}}>X</div>

Inline styles (object)

Components
<button onClick={() => setN(n + 1)}>+</button>

Event handler

Components
const [count, setCount] = useState(0);

State with initial value

Hooks
setCount(count + 1);

Update state

Hooks
setCount(c => c + 1);

Functional state update

Hooks
setUser({...user, name: 'Alice'});

Update object state (spread)

Hooks
setItems([...items, newItem]);

Append to array state

Hooks
useEffect(() => {
  fetchData();
}, []);

Run once on mount

Hooks
useEffect(() => {
  ...
}, [dep1, dep2]);

Run when deps change

Hooks
useEffect(() => {
  const id = setInterval(...);
  return () => clearInterval(id);
}, []);

Effect with cleanup

Hooks
const ref = useRef(null);
<input ref={ref} />

DOM ref

Hooks
const renders = useRef(0);

Mutable ref (no re-render)

Hooks
const value = useMemo(() => expensive(x), [x]);

Memoize computation

Hooks
const fn = useCallback(() => doThing(x), [x]);

Memoize callback

Hooks
const theme = useContext(ThemeContext);

Consume context

Hooks
const [state, dispatch] = useReducer(reducer, initial);

Reducer pattern

Hooks
function useDebounce(value, delay) { ... }

Custom hook

Hooks
const [email, setEmail] = useState('');
<input value={email} onChange={e => setEmail(e.target.value)} />

Controlled input

Forms
<input type="checkbox" checked={on} onChange={e => setOn(e.target.checked)} />

Controlled checkbox

Forms
<form onSubmit={e => { e.preventDefault(); submit(); }}>

Submit handler

Forms
<input ref={inputRef} defaultValue="hi" />

Uncontrolled input (ref)

Forms
const ThemeContext = createContext('light');

Create context

Context
<ThemeContext.Provider value="dark">
  <App />
</ThemeContext.Provider>

Provide context value

Context
<BrowserRouter>
  <Routes>
    <Route path="/" element={<Home />} />
  </Routes>
</BrowserRouter>

Basic routes

Routing
<Link to="/about">About</Link>

Navigation link

Routing
const navigate = useNavigate();
navigate('/home');

Programmatic navigation

Routing
const { id } = useParams();

Read URL params

Routing
const [params] = useSearchParams();

Read query string

Routing
const Button = React.memo(({ onClick }) => ...);

Memoize component

Performance
const Heavy = lazy(() => import('./Heavy'));
<Suspense fallback={<Loader/>}><Heavy /></Suspense>

Code splitting with Suspense

Performance
Showing 40 of 40 snippets

React Cheat Sheet — Hooks, Components, JSX & Context API Reference

React arrived in 2013 with a JSX view layer and a class-component model; the function-plus-hooks model that landed in 2019 has been the recommended approach since, and most React code today is closer to a list of pure render functions than to the original class lifecycle. The cheatsheet below covers 40+ patterns across components, hooks, forms, context, routing, and performance. Most trouble in real components doesn't come from forgetting JSX syntax. It comes from quirks that look ordinary but bite. A `useEffect` reading a state value without listing it in the dependency array reads the value from the render that created the effect, not the latest one. A list rendered without a stable `key` reuses DOM nodes in surprising orders during reorders. The snippets below are the ones an author reaches for when debugging exactly that — the dependency array that ends an effect loop, the functional updater that survives batching, the cleanup that stops a leaking interval — without rereading the React reference from line one each time.

Common pitfalls in React

A few patterns earn their place on the first screen of any React component. The functional state updater `setCount(c => c + 1)` should replace the value-form whenever the new state depends on the previous one — it survives batching and avoids the classic off-by-N counter bug. Effect dependency arrays should list every reactive value the effect reads, including props, state, and locally declared functions; the ESLint `react-hooks/exhaustive-deps` rule is worth keeping on, because the cases it catches are almost always real stale-closure bugs. Lists need a stable `key` from the data itself, not the array index — indexes shift on reorder and confuse the reconciler. Cleanup functions returned from `useEffect` matter for intervals, subscriptions, and event listeners, not just for unmount but for every re-run of the effect when its dependencies change. The cheatsheet groups all of this into Components, Hooks, Forms, Context, Routing, and Performance so the right section is one click away.

  • 40+ React patterns with JSX
  • Full hooks coverage (useState, useEffect, useRef, etc.)
  • Controlled forms and submit handlers
  • Context API and providers
  • React Router v6 routing
  • React.memo and lazy loading

Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).

By ·