Sean Chang
NTAD
Redux time travel
Use effects in UI component directly
F8 2014 (Facebook)
MVU: Model-View-Update
types
./src/store.js
./src/index.js
./src/features.js
What Redux Toolkit done for us
./src/selector.js
./src/container/Dashboard.js
So... How can we eliminate the side effects in Redux reducers?
Redux Middleware can do this!
Let's start from a simple log function...
Log before & after dispatching an action
DRY: don't repeat yourself
Monkeypatch: I'm too lazy to import this function every time!
We can have many patches
Hide mokeypatch
Remove monkeypatch: pass the next for me plz!
applyMiddleware
APICurrying!
You are able to write a redux middleware!
./src/store.js
Redux-Saga
Redux-Thunk
Side Effects Management
Redux-Thunk
"thunks" are a pattern of writing functions with logic inside that can interact with a Redux store's dispatch and getState methods.
Can be synchronize or asynchronize function
How do we call the API?
Redux-Saga
Generator function yields Effects (Promise-like objects)
Yield an object and let middleware execute
Factory functions creating Effects
take
: Take specific actions from middleware (blocking).call
: Send a blocking call request to middleware.put
: Send a store.dispatch
request to middleware (blocking).fork
: Send a non-blocking call request to middleware.Task cancellation
takeEvery
: a high-level API built using take and fork.takeLatest
: a high-level API built using take, fork and cancel.takeLatest
take, ...
. Register saga worker and listen on specific action type.call, put, ...
.Run jobs defined in worker function../src/saga.js
./src/store.js