Part 07 · State · 7.1.02

State snapshot, batching và immutable updates

Mỗi render nhìn một snapshot; setter yêu cầu render mới, không đổi biến state trong handler đang chạy.

Model rule: giữ source of truth nhỏ nhất; nếu value tính được từ props/state hiện tại thì derive trong render thay vì đồng bộ bằng effect.

1. Snapshot và update queue

Event handler capture state snapshot của render đã tạo nó. React enqueue update và batch trước render tiếp theo. Value update thay pending result; functional updater nhận kết quả trước trong queue, nên dùng khi next phụ thuộc previous hoặc có nhiều update.

setCount(count + 1); setCount(count + 1); // cùng snapshot
setCount(c => c + 1); setCount(c => c + 1); // compose queue

2. State model và ownership

Tránh redundant, duplicate và contradictory state. Lift state tới owner chung nhỏ nhất; không mirror props nếu không có semantics reset rõ. Dùng reducer/state machine hoặc discriminated union khi các trạng thái loại trừ nhau.

SmellBetter model
isLoading + error + data độc lậpUnion idle/loading/success/error
Filtered list trong stateDerive từ items + filter
Hai component giữ cùng selectionLift tới common owner
Prop copied once vào stateControlled value hoặc intentional key/reset contract

3. Object và array identity

State được xem read-only: tạo object/array mới cho changed path, không mutate alias. Shallow spread không clone nested graph; normalize state hoặc copy đúng path. Immer dùng proxy để produce immutable result nhưng vẫn cần hiểu identity và không leak draft.

4. Priority và responsive updates

Transitions đánh dấu non-urgent update có thể interrupt; controlled input urgent không đặt trong transition. Deferred value cho subtree chậm render với value lagging nhưng không thay debounce, request cancellation hoặc server cache.

Evidence: test queue bằng multiple updates, test reorder/identity, dùng Profiler đo urgent interaction và xác nhận transition không che network race.
Tài liệu: State as a Snapshot · Queueing State Updates · Choosing State Structure