Free React cheat sheet

React cheat sheet

Modern React on one page: components, JSX, props, useState, useEffect, lists and keys, and event handling.

Components and JSXPropsState with useStateEffects with useEffectLists and keysEvents and forms

← All cheat sheets

React Cheat Sheet

Quick reference from FreeCodingCourses.com

Components and JSX

function Hello() { return <h1>Hi</h1> }
a component is a function that returns JSX.
<Hello />
use it like an HTML tag. Component names are capitalized.
<div className="card">
class is className in JSX.
<p>{2 + 2}</p>
curly braces drop JavaScript into JSX.
return <>{a}{b}</>
a fragment groups siblings without a wrapper div.

Props

function Card({ title, children }) { ... }
props come in as one object; destructure them.
<Card title="Hello" count={3} />
strings in quotes, everything else in braces.
<Card>text</Card> // props.children
content between tags is children.
function Card({ title = 'Untitled' })
default value for a missing prop.

State with useState

const [count, setCount] = useState(0)
state value plus its setter, initial value 0.
setCount(count + 1)
update; React re-renders the component.
setCount(c => c + 1)
functional update, safest when the new value depends on the old.
const [user, setUser] = useState(null)
state can hold anything: object, array, null.

Effects with useEffect

useEffect(() => { ... }, [])
run once after first render (empty deps).
useEffect(() => { ... }, [id])
re-run whenever id changes.
useEffect(() => { return () => cleanup() }, [])
return a cleanup function.
// fetch, subscriptions, timers go here
effects are for talking to the outside world.

Lists and keys

{items.map(i => <li key={i.id}>{i.name}</li>)}
render a list with map.
key={i.id}
a stable, unique key per item. Not the array index if the list changes.
{loading && <Spinner />}
render conditionally with &&.
{ok ? <Yes /> : <No />}
either/or with a ternary.

Events and forms

<button onClick={handleClick}>
pass the function, do not call it.
<input value={name} onChange={e => setName(e.target.value)} />
controlled input tied to state.
<form onSubmit={e => { e.preventDefault(); ... }}>
stop the page reloading on submit.

Unlock the full React cheat sheet

You're seeing 2 of 6 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.

No spam, unsubscribe anytime. See our privacy policy.

React questions, answered

Do I need to learn JavaScript before React?

Yes, and it is the most common reason beginners struggle with React. React is a JavaScript library, so array methods like map, arrow functions, destructuring, and the spread operator all show up constantly. Get comfortable with modern JavaScript first.

What is a hook?

A hook is a function starting with use that lets a component remember state and run side effects. useState gives a component memory between renders; useEffect runs code after render, like fetching data. Hooks only work inside components, at the top level, never in loops or conditions.

Why does my list show a key warning?

React wants a unique key prop on each element you render from an array so it can track which item is which. Add key={item.id} to the outermost element inside your map. Avoid using the array index as the key if the list can reorder or change length.

Where to go next