Skip to content
ClapelloNotes by Barath
ReactCh 01 · 1/3
Beginner3 min readUpdated

React / 01

What is React?

TL;DR

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 }
data changed

2 · react

Compares

old UI vs new UI → finds the one difference
patch only that

3 · the screen

Updates

4 likes
You change the data. React works out what changed and updates only that part of the screen.

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

in charge

Your code

decides what happens and when
you call it

The library

does one job when asked

Framework

Angular · Next.js

in charge

The framework

owns the structure and the flow
it calls you

Your code

fills in the blanks it leaves
Inversion of control: with a library you hold the steering wheel. With a framework, it drives and calls you when needed.
  • 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.