Redux Basics Flashcards
What is Redux approach on where to store the state of the application?
All state is store in a single state object, also called the state tree.
What is the first principle of Redux?
Everything that changes in the application, including data and UI state is stored in a single state object. The state tree.
What is the second principle of Redux?
The state tree is immutable. The only way to change it is by dispatching Actions.
Who can dispatch Actions?
Anyone that wants to change the app state: network events, user interactions on the UI, timers
What are Redux Actions?
They are plain Javascript objects which at a minimum say what change happened in the app. Ex:
{ type: “COMPLETE_TODO”, id: “1”}
What is the third principle of Redux?
To describe state mutation you have to write a pure function that takes the previous state of the application, the Action that will mutate it, and return the new state of the application. This is the reducer function.
Being pure, what are the constraints of the reducer function?
- cannot create side effects
- must depend only on the parameters given
- must not change the parameters
- must return a new state object
What should the reducer function do if it receives undefined as previous state?
It should return the application initial state.
Which concept from Redux joins the 3 principles together: state tree, action dispatch and reducer function?
The Store object
What are the main methods from the store object?
getState()
dispatch(action)
subscribe(callback)
How to integrate react with Redux?
On Redux subscriber you call react render. On react event callbacks you dispatch mutation events to Redux.
What is the JS ES5 function that makes an object immutable?
Object.freeze()
In Redux, what is the correct way of changing an array part of state?
By creating a copy of the array, using the concat(), slice() or the ... spread operator. Ex: var a = [1,2,3] return a.slice(1); // [2,3] return [...a, 4] // [1,2,3,4] return a.concat(4) // [1,2,3,4]
What is a presentational component?
Component that only concerns in how it looks. Every UI action is received via props callbacks from above.
What are the pros and cons of presentational components?
Pro
- decouples most components from redux
Con
- requires callbacks to be passed down until leaves via props