Based on:
- https://redux-toolkit.js.org/tutorials/quick-start
- https://github.com/reduxjs/cra-template-redux/blob/master/template/src/features/counter/
- If you already have an App, only add the two important dependencies
yarn add "@reduxjs/toolkit react-redux
- If you don't have an App, create new app with CRA w/
cra-template-redux
- Add store.
- Add store provider to
<App>
component.
Each time, you want to add a new data point or CRUD operation, do these steps:
- Add service
- Add slice
- e.g. https://github.com/reduxjs/cra-template-redux/blob/master/template/src/features/counter/counterSlice.js
- NOTE: slices are reducers
- Add thunk to wrap service
- Add the new thunk to slice via
extraReducers
. - Add slices/reducers to store
See: https://redux-toolkit.js.org/tutorials/quick-start#use-redux-state-and-actions-in-react-components
- Read:
useSelector(mySelector)
mySelector
is a selector function defined near (NOT in!) your slice
- Write:
const dispatch = useDispatch(); ... dispatch(myAction());
myAction
is either a thunk, returned bycreateAsyncThunk
or- any of your
slice
'sreducers
- any of your
- Entities - https://redux-toolkit.js.org/api/createEntityAdapter
- Caching
- Indexing
- Real-time events
- Add
immer
- https://redux-toolkit.js.org/usage/immer-reducers- Goals:
- automatically propagate deep state updates
- protect state from accidental modification by automatically freezing it
- Goals: