Kevin C.
I love sharing my learning journey in the technology land and hopefully you can also takeaway some interesting tidbits from mine for your own learning and inspire many others to pass on these knowledges. Enjoy and happy learning & sharing!
Describe UI as a function of state
Emits state updates in response to actions
Render UI
Manage State
console.log(store.getState())
//Prints below
{
visibilityFilter: 'SHOW_ALL',
todos: [
{
text: 'Consider using Redux',
completed: true,
},
{
text: 'Keep all state in a single tree',
completed: false
}
]
}
state of your whole application is stored in an object tree
store.dispatch({
type: 'COMPLETE_TODO',
index: 1
})
store.dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: 'SHOW_COMPLETED'
})
Emit an action to mutate state
const todo = (state, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
completed: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
}
return {
...state,
completed: !state.completed
};
default:
return state;
}
};
Pure reducer(s) specify how the state tree is transformed by action
UI Re-render
Container Components | Presentational Components | |
---|---|---|
Location | Top Level, route handlers | Middle and leaf components |
Aware of Redux | Yes | No |
To read data | Subscribe to Redux state | Read data from props |
To change data | Dispatch Redux actions | Invoke callbacks from props |
Redux | Flux | |
---|---|---|
How | Split the root reducer into smaller, hierarchical, manageable pieces | Add more stores since they are usually flat, and not connected |
Design Pattern | Functional Composition | Callback Registration |
Result | Like nested React components, it reduces boilerplate code and makes them easier to reuse & port | Store sprawl with lots of non-reusable boilerplate code |
Scaling the code when application grows
image source:[https://code-cartoons.com/a-cartoon-intro-to-redux-3afb775501a6#.3mir3rhiq]
Redux | Flux | |
---|---|---|
How | Stores and Actions are just pure functions. | How to test Stores? React Hot Loader breaks Flux stores is edited |
Result | They are easily testable in isolation. Call them, and make assertions on what they return. Can also mock the Redux store or HTTP requests for tests |
Cannot test the functionalities involved in data flow easily Mocking is difficult |
Testing the code with Unit Tests
Redux Principles
Redux Data flow
Redux Vs Flux
By Kevin C.
This sharings introduce the use of Redux (predictable state container for JavaScript apps) in the modern React Web Application.
I love sharing my learning journey in the technology land and hopefully you can also takeaway some interesting tidbits from mine for your own learning and inspire many others to pass on these knowledges. Enjoy and happy learning & sharing!