Skip to main content

JavaScript for React — visual mental model

React is a JavaScript library. Before memorising React APIs, build the JavaScript models that React code depends on.

The JavaScript foundation under ReactReact syntax becomes much easier when these ordinary JavaScript concepts are already familiar.
Data
objects · arrays · destructuring · spread
Transformation
map · filter · find · reduce · derived values
Functions
callbacks · closures · scope · modules
Identity
primitive values · references · object equality
Async
Promises · async/await · event-loop basics
Expressions
ternaries · logical operators · optional chaining · nullish coalescing

Arrays become UI​

Array data → React list UI
Array of domain data
products
Transform data into element descriptions
products.map(product => <ProductCard ... />)
Visible list
Each item becomes part of the component tree.

Immutable updates​

Mutation vs immutable updateReact state works best when previous snapshots remain unchanged.
MutationChange the existing value

items.push(newItem) keeps the same array object and modifies it in place.

Immutable updateCreate the next value

[...items, newItem] creates a new array while the previous snapshot stays intact.

Reference identity​

Objects compare by identity, not by shape
Primitive value
5 === 5 → true
Object reference
{} === {} → false
Effects
dependency identity
Context
provider value identity
Memoization
memo/useMemo/useCallback
External stores
selector result identity

Closures and render snapshots​

Why React callbacks can remember older valuesThe closure is JavaScript; the snapshot boundary comes from React rendering.
1Render starts
2Props/state values exist for this render
3Handlers/functions are created
4Functions close over those values
5Later callback runs with that closure

Async work​

Async JavaScript does not block the whole runtime
Current JavaScript runs
Promise represents a future result
pending → fulfilled or rejected
Continuation becomes eligible later
await pauses the async function, not the entire JavaScript runtime.

Which JavaScript tool fits?​

What are you trying to do?
Render one UI item for every array item?map
Keep only matching items?filter
Find one matching item?find
Calculate a total or aggregate?reduce or a clearer dedicated operation
Copy while changing an object/array?spread + immutable update
Run code later after another operation?callback / Promise / async function
Reuse code across files?ES modules

Keep this picture in your head​

JavaScript → React
JavaScript values + functions
React components use those values to describe UI
React adds rendering, state, identity, and lifecycle semantics

Continue with the detailed JavaScript for React chapter for the complete examples, edge cases, closures, promises, modules, and event-loop explanation.