Redux Flashcards
What is the purpose of redux?
To manage state and to make state more predictable
What are the benefits of redux over native state management?
The data is separated from the UI and lives outside of the UI.
All of the data is located in one place and needs to be only changed once
This makes it easier to share data between components
Makes State more predictable
How does Redux store data?
In a state tree
what is the only thing that can change the state of a store?
Only an event can change the state of a store
What are “Actions”?
Actions are events that change the state of the store.
They are objects that contains a type, property and the state that needs to be mutated.
example:
{ type:"ADD_GOAL", goal: { id: 0, name: "Run 5km" } }
What are action creators?
Action Creators are functions that create/return action objects
example:
const addItem = (item)=>{ return { type:"ADD_ITEM", item } }
Rules
- ONLY an event can change the state
2. The function that returns the new state needs to be a pure function
Pure Functions
- Always returns the same result if the same arguments are passed in
- Depends solely on the arguments passed into them
- Does not produce side effects (no interaction with the outside world)
// square()
is a pure function
const square = x => x * x;
What is the main benefit of pure functions?
the most important feature of a pure function is that it’s predictable. If we have a function that takes in our state and an action that occurred, the function should (if it’s pure!) return the exact same result every single time.
What does the .dispatch method do?
it updates the state in the store by calling the reducer and then invoking any associated subscriptions.
Which three methods are available on the store?
.getState() -return state
.subscribe() -notifies when state changes
.dispatch(state, action) - updates the state
Which store method connects react components to the store?
.subscribe()
How to create a store with more than one reducer?
combine mutliple reducers into one root reducer like so:
function root (state = {},action){ return{ todos:todos(state.todo,action), goals:goals(state.goals,action) } }
Which function to call on the Redux library to combine multiple reducers?
Redux.combineReducers({
todos,
goals
}))
How to add middlewear into redux
const store = Redux.createStore( , Redux.applyMiddleware() )