Application State with Redux

My reading notes for Code Fellows


Application State with Redux

1. What is the first principle of Redux?

One may represent the whole state of the application as a single javascript object. All mutations and changes to the state are explicit so it is possible to keep track of all of them.

2. What is a store and what do we use our reducers for within that store? Source]

The Redux store brings together the state, actions, and reducers that make up the app. A store holds the whole state tree of your application. The only way to change the state inside it is to dispatch an action on it.

A store is not a class. It’s just an object with a few methods on it.

Reducers are functions that take the current state and an action as arguments, and return a new state result.

3. Name three Redux store methods given to us by createStore and describe their use. Source

getState() Returns the current state tree of your application. It is equal to the last value returned by the store’s reducer.

dispatch(action) Dispatches an action. This is the only way to trigger a state change.

subscribe(listener) Adds a change listener. It will be called any time an action is dispatched, and some part of the state tree may potentially have changed. You may then call getState() to read the current state tree inside the callback.

replaceReducer(nextReducer) Replaces the reducer currently used by the store to calculate the state.

4. Explain to a non-technical recruiter what combineReducers() does and why it is useful. CombineReducers generates one reducer from several other reducers, which is then passed to the createStore method. The combineReducers helper function turns an object whose values are different reducing functions into a single reducing function you can pass to createStore. Once the combined reducers are passed to createStore(as the root reducer), they can be accessed by using dot notation state.key.value.

It’s like a drawer that holds all sorts of things, rather than having those same things in different parts of the house. You’ll know that if you want to access those things, you can do so from one neatly organized drawer.