Redux Flashcards
What are the parts of the Flux data flow?
Action > Dispatcher > Store > View
What is Flux?
A pattern that uses a unidirectional data flow
What are the parts of the Redux data flow?
Action > Store > View (only one store, so no need for dispatchers)
What are the 3 core principles of Redux?
- Single source of truth
- State is read-only
- Changes are made with pure functions
What are the minimum requirements for sending an action in Redux?
- It must be an object 2. It must have a type property
At a minimum, what argument(s) does redux.createStore method require?
The reducer function used by the store; optionally, an initial state object
How do you change state when using redux store?
Use store.dispatch(); // requires an action
How can you add redux store to localStorage every time state is updated?
store.subscribe( () => { const state = JSON.stringify(store.getState()) localStorage['react-store'] = state })
How can you get initial state from local storage and set in Redux store?
const initialState = localStorage[‘redux-store’] ? JSON.parse(localStorage[‘redux-store’]) : { } // Then pass initialState as second argument to createStore()
Where does logic belong in a Redux app?
Action Creators, functions that create and return actions
How do you associate middleware with your store (how do you get it to run?)
Use the applyMiddleware() function from ‘redux’
How does myStore.subscribe() work?
It listens for any time an action is dispatched to the store; it takes an optional callback function – e.g. to update localStorage.
How do you pass the store down the tree of components?
Use the redux Provider component.
How do you map state from store to properties in a child component?
Use the connect() method.
How does the redux connect method work?
connect(mapStateToProps cb function)(nameOfComponent) // It takes a mapping object and requires a component