AI Generate React docs instantly

React Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Components

2 snippets

Function components and props

Basic Component

function Greeting({ name, children }) {
  return (
    <div>
      <h1>Hello {name}</h1>
      {children}
    </div>
  );
}

<Greeting name="World"><p>Welcome!</p></Greeting>

TypeScript Props

interface ButtonProps {
  variant?: 'primary' | 'secondary';
  onClick: () => void;
  children: React.ReactNode;
}

function Button({ variant = 'primary', onClick, children }: ButtonProps) {
  return <button className={variant} onClick={onClick}>{children}</button>;
}

Hooks

5 snippets

State and side effects

useState

const [count, setCount] = useState(0);
setCount(5);           // Direct value
setCount(prev => prev + 1);  // Functional update

const [user, setUser] = useState<User | null>(null);

useEffect

// Run on mount
useEffect(() => {
  fetchData();
}, []);

// Run when dependency changes
useEffect(() => {
  document.title = name;
}, [name]);

// Cleanup
useEffect(() => {
  const id = setInterval(tick, 1000);
  return () => clearInterval(id);
}, []);

useRef

const inputRef = useRef<HTMLInputElement>(null);
inputRef.current?.focus();

// Mutable value (no re-render)
const renderCount = useRef(0);
renderCount.current++;

useMemo / useCallback

const filtered = useMemo(
  () => items.filter(i => i.active),
  [items]
);

const handleClick = useCallback(
  () => setCount(c => c + 1),
  []
);

useReducer

function reducer(state, action) {
  switch (action.type) {
    case 'increment': return { count: state.count + 1 };
    case 'reset': return { count: 0 };
    default: throw new Error();
  }
}
const [state, dispatch] = useReducer(reducer, { count: 0 });

Context

2 snippets

Share state across components

Create & Provide

const ThemeContext = createContext<'light' | 'dark'>('light');

function App() {
  const [theme, setTheme] = useState('light');
  return (
    <ThemeContext.Provider value={theme}>
      <Page />
    </ThemeContext.Provider>
  );
}

Consume

function Button() {
  const theme = useContext(ThemeContext);
  return <button className={theme}>Click</button>;
}

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Common Patterns

4 snippets

Lists, conditionals, forms

Conditional Rendering

{isLoggedIn && <Dashboard />}
{error ? <Error msg={error} /> : <Content />}
{status === 'loading' && <Spinner />}

Lists

{items.map(item => (
  <li key={item.id}>{item.name}</li>
))}
// Never use index as key if list can reorder

Controlled Form

const [email, setEmail] = useState('');
<form onSubmit={(e) => { e.preventDefault(); submit(email); }}>
  <input value={email} onChange={(e) => setEmail(e.target.value)} />
  <button type="submit">Send</button>
</form>

Custom Hook

function useLocalStorage<T>(key: string, initial: T) {
  const [value, setValue] = useState<T>(() => {
    const stored = localStorage.getItem(key);
    return stored ? JSON.parse(stored) : initial;
  });
  useEffect(() => {
    localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);
  return [value, setValue] as const;
}

Performance

2 snippets

Optimization techniques

React.memo

const ExpensiveList = React.memo(function ExpensiveList({ items }) {
  return items.map(i => <Item key={i.id} {...i} />);
});
// Only re-renders when items change

Lazy Loading

const AdminPanel = React.lazy(() => import('./AdminPanel'));

function App() {
  return (
    <Suspense fallback={<Spinner />}>
      <AdminPanel />
    </Suspense>
  );
}

More Cheat Sheets

FAQ

Frequently asked questions

What is a React cheat sheet?

A React cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in React. It helps developers quickly look up syntax without searching through documentation.

How do I learn React quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important React concepts?

Key React concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my React code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your React code using AI.

Related resources

Stop memorizing. Start shipping.

Generate React Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week