Part Two: Scable Redux Application
The space the framework was built to solve
The space that framework doesn't solve or have an opinion on
“Don’t fall into the trap of thinking a library should prescribe how to do everything.”
“The perfect is the enemy of the good.”
store
action
reducer
One Fat Reducer VS. Splitted Thin Reducers
Problems & Solutions:
Get necessary data in action creators, and pass the data through actions.
Use high order functions to generate reducers.
Duplicated and Nested Structure
Using 'byId and allIds' Pattern
Using ImmutableJS Record to Validate Entity Shape
Problems & Solutions:
Use selectors to encapsulate the state shape and make the selecting reuseable.
Simply pass item IDs to connected children is a good pattern for optimizing UI performance
Pros:
Cons:
Encapsulating the Redux State Tree
Reducers、action creators encapsulate the writing to the redux state tree.
Reading from the redux state tree is scattered through mapStateToProps(), action creators, sagas and tests. It's hard to update the state shape.
Use selectors to encapsulate the reading from the redux state tree.
store
reducer
Redux
state
action
UI
DOM Event
Network
……
Reading state
Reading state
state.todos
selectAllTodos(state)
state.todos.filter((todo) => !todo.complete).length
selectActiveTodosCount(state)
Using Reselect to Compose Selectors and Memorize the Results
Naming action creators, thunks or sagas with 'getSomethingIfNeeded' pattern
Where to put business logic
×
×
√
√
Commands and Events
Commands:
Events:
Where to put business logic
×
Where to put business logic
×
Where to put business logic
√
Where to put business logic
√
Where to put business logic
√
A saga is
Where to put business logic
√
store
reducer
Redux
state
action
UI
DOM Event
Network
……
store
reducer
Redux
state
action
UI
DOM Event
Network
……
action
Saga
Saga
Worker1
Worker2
Worker3
action
action
Task
Result
Task
Result
Task
Result
Generator and Runner