React / 01
What is React?
A JavaScript library that keeps the screen in sync with your data. You describe the UI, and React does the updating.
React reacts
The name is the whole idea. Your app has data: a like count, a list of messages, the items in a cart. When that data changes, React reacts by working out what's different and updating only that part of the screen.
1 · your data
State
{ likes: 3 → 4 }2 · react
Compares
3 · the screen
Updates
Imperative vs declarative
These two words come up in every React interview. Here's the difference using a like button:
Don't
Imperative: you do every step
const likes = document.getElementById("likes");
const n = Number(likes.textContent) + 1;
likes.textContent = n;
if (n >= 10) button.disabled = true;You find, read, change and check things yourself, and you have to remember to do it everywhere.
Do
Declarative: you describe the result
<button disabled={likes >= 10}>
♥ {likes}
</button>"For this many likes, the button looks like this." React handles the rest.
That HTML-looking code is called JSX. It gets its own chapter later. For now, just notice the shift: you stop giving instructions and start giving descriptions.
Library or framework?
Both are code someone else wrote so you don't solve common problems from scratch. The difference is who's in charge.
Library
React · jQuery · Lodash
Your code
The library
Framework
Angular · Next.js
The framework
Your code
- Library: you call it, when you decide. You stay in control of your app's flow.
- Framework: it gives you the structure and calls your code at the right moments. You fill in the blanks.
That flip is called inversion of control, and it's the one-line answer interviewers are looking for.
React is a library: it only handles the UI layer. You can drop it into one corner of an existing page, and it won't take over the rest.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
