React Basics
React is the most popular UI library in JavaScript. Build interfaces from small, reusable components. Master props, state, effects, and event handling — the core building blocks of every React application.
What is React?
React is a JavaScript library for building user interfaces from components: functions that return JSX (HTML-like syntax) based on their props and state. When state changes with useState, React re-renders the component and updates only what changed in the DOM. It's the most widely used front-end library, and frameworks like Next.js build on it.
import { useState } from "react";
function Counter({ start = 0 }) {
const [count, setCount] = useState(start);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}Core React Hooks
| Hook | Purpose | Example Usage |
|---|---|---|
useState | Local mutable state | const [count, setCount] = useState(0) |
useEffect | Side effects after render | useEffect(() => { fetch('/api') }, [id]) |
useRef | Mutable ref not tied to render | const inputRef = useRef<HTMLInputElement>(null) |
useCallback | Memoize a function | const fn = useCallback(() => doX(a), [a]) |
useMemo | Memoize an expensive value | const sorted = useMemo(() => arr.sort(), [arr]) |
useContext | Read context value | const theme = useContext(ThemeContext) |
useReducer | Complex state transitions | const [state, dispatch] = useReducer(reducer, init) |
useId | Stable unique ID for a11y | const id = useId() |
Common Pitfalls
Interactive Demo
Code Editor
<div id="root"></div>Preview
React Basics: summary
Key points
- Props flow down from parent to child; children notify parents through callback props.
- Never mutate state directly; create new objects and arrays (
[...items, newItem]). useEffectis for syncing with things outside React (subscriptions, timers), not for deriving data.- List items need a stable
keyprop.
Frequently asked questions
›Should I learn JavaScript before React?
Yes. React is JavaScript: array methods, destructuring, modules, closures and async code are used everywhere in it.
›Is React a framework?
React is a UI library. Frameworks such as Next.js and Remix add routing, data loading and server rendering on top of it.