What is React, and why is it called a library rather than a framework?
React is a JavaScript library for building user interfaces from components: functions that take data and return what the UI should look like. You describe the UI declaratively for the current state, and React updates the DOM when that state changes. It's a library because it only handles the view: routing, data fetching, forms and global state come from packages you choose. A framework such as Angular ships all of those and decides your app's structure.
Full answerHide full answer2 min read
Declarative vs imperative
// Imperative, plain JavaScript: you change the DOM step by step
const button = document.querySelector("#like");
let likes = 0;
button.addEventListener("click", () => {
likes += 1;
button.textContent = `Likes: ${likes}`;
});
// Declarative, React: describe the UI for any value of likes, and React updates the DOM
function LikeButton() {
const [likes, setLikes] = useState(0);
return <button onClick={() => setLikes(likes + 1)}>Likes: {likes}</button>;
}With two or three pieces of state, the imperative version quickly becomes a tangle of "if this changed, also update that". The declarative version stays the same size.
Library vs framework
| Concern | React (library) | Angular (framework) | Next.js (framework built on React) |
|---|---|---|---|
| Rendering the UI | Yes | Yes | Yes, via React |
| Routing | Pick one: React Router, TanStack Router | Built in | Built in: file-based |
| Data fetching | Pick one: fetch, TanStack Query, SWR | Built in: HttpClient | Built in: server components, caching |
| Forms | Pick one, or plain state | Built in | React's, plus server actions |
| Project structure | Up to you | Prescribed | Prescribed folders |
The informal test: you call a library's code; a framework calls yours.
The ideas React is built on: components, one-way data flow (question 4), a virtual DOM with reconciliation (question 2), and hooks for state and side effects.
Follow-ups
- Is Next.js a library or a framework? A framework built on React: it adds routing, server rendering, data fetching and build tooling (question 32).
- What is React Native? The same component model, rendering to native iOS and Android views instead of the browser DOM.