Store lưu toàn bộ state tree trong một object duy nhất — dùng configureStore() (RTK) thay vì createStore() (legacy); useSelector/useDispatch là interface chính cho React components.
- Store là object trung tâm lưu trữ toàn bộ state tree của ứng dụng, được tạo bằng
configureStore()(RTK) hoặccreateStore()(legacy). - Ba phương thức chính:
getState()trả về state hiện tại,dispatch(action)gửi action qua middleware rồi đến reducer để cập nhật state,subscribe(listener)đăng ký callback được gọi sau mỗi dispatch. - Trong thực tế với React, bạn hiếm khi gọi trực tiếp các phương thức này vì
react-reduxđã wrap chúng quauseSelector(thay getState + subscribe) vàuseDispatch(thay dispatch). - Lưu ý: chỉ nên có MỘT store duy nhất trong app — nhiều store phá vỡ nguyên tắc single source of truth và làm DevTools không hoạt động đúng.
- Với RTK,
configureStoretự động setup Redux DevTools, thunk middleware, và development checks (phát hiện accidental mutation).
The store holds the entire state tree in a single object — use configureStore() (RTK) instead of createStore() (legacy); useSelector/useDispatch are the primary interface for React components.
- The store is the central object holding the entire application state tree, created with
configureStore()(RTK) orcreateStore()(legacy). - Three main methods:
getState()returns the current state,dispatch(action)sends an action through middleware then to the reducer to update state,subscribe(listener)registers a callback called after every dispatch. - In practice with React, you rarely call these methods directly because
react-reduxwraps them viauseSelector(replaces getState + subscribe) anduseDispatch(replaces dispatch).
Pitfall: there should only be ONE store in an app — multiple stores break the single source of truth principle and prevent DevTools from working correctly.