Frontend
React
A library for building interfaces from components: UI as a function of state.
What it is
React was created at Facebook (now Meta) and open sourced in 2013. The UI is built from components: functions that take props and return markup (JSX).
The key idea: you describe what the UI looks like for the current state, and React updates the DOM by diffing the new tree against the previous one (reconciliation). State and side effects in components work through hooks (useState, useEffect).
React is only the view layer: routing, data and builds come from the ecosystem. The most common framework around React is Next.js, and React Native covers mobile apps.
Use it when
- A complex interactive interface with lots of state.
- You want the largest library ecosystem and the biggest frontend job market.
- You want to share knowledge between web (React) and mobile (React Native).
- A team project where the component approach helps reuse.
Avoid it when
- A simple content site: static generation or Astro ships less JS.
- The team wants a batteries-included solution without picking libraries: look at Angular or Vue.
Key concepts
- Component and props
- A function that returns UI. Props pass data from parent to child.
- State and hooks
useStateholds local state,useEffectsyncs with the outside world,useMemoanduseCallbackcache.- Virtual DOM / reconciliation
- React compares the new element tree with the previous one and applies minimal changes to the DOM.
- Keys
- Stable identifiers of list items that React uses to match them between renders.
- Server Components
- Components that run on the server and add no JS to the browser (in the Next.js App Router).
Example
Cheat sheet
| Command or code | What it does |
|---|---|
npm create vite@latest app -- --template react-ts | New React project with TypeScript |
const [n, setN] = useState(0) | Local state |
setN(prev => prev + 1) | Update state from the previous value |
useEffect(() => { ... }, [dep]) | Side effect that depends on dep |
useRef(null) | Reference to a DOM node or a mutable value |
useMemo(() => calc(a), [a]) | Caches a computation between renders |
useCallback(fn, [dep]) | Stable function reference |
{items.map(i => <li key={i.id}>{i.name}</li>)} | List rendering with a key |
<Ctx.Provider value={v}> | Passes a value through context |
export default function Card({ title }) { } | Component with props |
How to learn it
- First take the JavaScript course, especially arrays, destructuring and async.
- Take the React course on ShortLesson and build your first component.
- Learn props, state with useState and conditional list rendering.
- Study useEffect, when you need it and when you do not.
- Learn to extract logic into custom hooks and to work with forms.
- Add routing and API requests, for example with TanStack Query.
- Build a small app with Vite and TypeScript.
- Move to Next.js when you need SSR and file based routing.
Interview questions
1. Why are keys needed in lists?
A key helps React match items between renders so it can add, remove and reorder them correctly without losing state. The key should be a stable id, not an array index, when the list changes.
2. How does useEffect differ from an event handler?
A handler responds to a user action.
useEffectruns after render to sync the component with an external system (request, subscription, timer). Cleanup is returned from the effect.3. What is a controlled component?
A form field whose value comes from React state through
valueand changes throughonChange. State is then the single source of truth.4. When does a component re-render?
When its state or props change, or its parent re-renders. For optimization use
React.memoanduseMemo, but measure first.
Interview questions from the bank
- What are the rules of hooks, and why can't you call them inside conditions or loops?JuniorReact
- What does StrictMode do, and why do components render twice in development?JuniorReact
- What is
useRefused for, and how does a ref differ from state?JuniorReact - What is lifting state up, and when is it better to keep state closer to where it is used?JuniorReact
- Why can't you mutate state in React, and how should objects and arrays be updated correctly?JuniorReact
- How does reconciliation work, and what heuristics does React use when comparing trees?MiddleReact
- How do you reset a component's state with
key, and when is it better than useEffect?MiddleReact - When does
React.memohelp, and when does it only hurt?MiddleReact
Comparisons with this technology
Related technologies
- Next.jsA full-stack React framework: routing, SSR, static generation, API routes and server components.
- React NativeA framework for native mobile apps using React and JavaScript or TypeScript.
- ReduxA predictable state container: a single store, changes only through actions and reducers.
- TanStack QueryA library for server data: queries, caching, background refetching and mutations.
- TypeScriptA superset of JavaScript with static types that compiles to plain JS.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).