What is React?
Solution and explanation
What it is
React is a JavaScript library for building user interfaces out of components. You describe what the screen should look like for the current data (state and props), and React works out the smallest set of DOM changes needed to get there. It was released by Facebook (Meta) in 2013 and is used by Facebook, Instagram, Netflix, Swiggy, Flipkart and most modern dashboards.
- **Declarative:** you write
if (loading) return <Spinner/>, not "find the div, remove its children, append a spinner". - **Component-based:** a page is a tree of small, reusable pieces (
<JobCard>,<SearchBar>,<Pagination>). - **One-way data flow:** data goes down from parent to child through props; children ask parents to change data through callback props.
- **Library, not framework:** React only handles the view. Routing (React Router), data fetching (TanStack Query), forms and global state come from other libraries or frameworks such as Next.js.
A first component
import { useState } from 'react';
export default function JobCounter() {
const [applied, setApplied] = useState(0);
return (
<div className="card">
<p>You applied to {applied} jobs today</p>
<button onClick={() => setApplied(applied + 1)}>Apply</button>
</div>
);
}
Each click calls setApplied; React re-runs the function, compares the new output with the old one and updates only the text inside <p>.
How rendering works
- **Render:** React calls your component functions to get a tree of elements.
- **Reconcile:** it compares (diffs) the new tree with the previous one.
- **Commit:** it applies the minimal DOM changes, then runs effects.
Interview points
- React is a UI library; Angular is a full framework with DI, router and forms built in.
- It is fast mainly because it batches updates and touches the real DOM as little as possible.
- React 18 added concurrent rendering (
startTransition), automatic batching and streaming SSR.
Summary
React = components + state + declarative rendering. You describe the UI for each state, and React keeps the DOM in sync.
