w7d1 - redux Flashcards
Three principles of Redux
Single Source of Truth
State is Read-Only
Only Pure Functions Change State
What does Single Source of Truth mean?
The entire state of the application is stored in a single JS object in a single store, commonly referred to as a ‘state tree’.
What does State is Read-Only mean?
The only way to change the state is to dispatch an action. This principle ensures that our Redux loop is never short-circuited and change of state remains single-threaded.
What is the Redux loop?
Action -> Dispatcher -> Store -> View
Optionally a View may create an Action that loops back to the Dispatcher, Store and View in that order.
What does the Store do?
It holds the global state of an application.
How do you create a Store in redux?
createStore(reducer, [preLoadedState], [enhancer])
What does the reducer do?
The reducer is a reducing function that receives the app’s current state and incoming actions, determines how to update the store’s state, and returns the next state
What does the preloadedState argument for createStore do?
It’s an optional object representing any application state that existed before the state was created.
What does the preLoadedState argument for createStore do?
It’s an optional function that adds extra functionality to the store
How do you get the current store’s state?
store.getState( )
How do you pass an action into the store’s reducer?
dispatch( action )
How do you register a callback to be triggered whenever the store updates?
subscribe( callback )
What’s unique about callbacks registered to the store?
Once they’re called, they’re unsubscribed, and so they will only be called once.
What exactly is a redux action?
a POJO with a mandatory ‘type’ key and optional payload keys that contain new information to be merged into the store.
How do you set up a basic redux action containing an orange with type ADD_FRUIT?
const addOrange = { type: "ADD_FRUIT", fruit: "orange" };