Learn Redux - Wes Bos Flashcards
Within React Router, what is an IndexRoute?
When routes are nested, declaring a route as an IndexRoute instead of just a Route means that if the base url is visited, the IndexRoute is the route which will be fed to the main parent by default, the other will have to be explicitly visited.
What is the main difference with Redux over plain React?
We keep all of our data within a single Store rather than holding our state in a bunch of component states. We have one giant object that contains all of our data for all of our store.
How do we see what is in our store (from within the console, say)
Provider.store.getState()
What are the two core components behind working with Redux?
Reducers and Actions
Where do we create our store.js file?
Within our client folder (not within the components folder)
What is the benefit of using syncHistoryWithStore?
We can hook up ReactRouter with our Redux instance.
Why do we import browserHistory from React Router into our Redux Store instead of directly into our main app file?
Because we will need to modify its behavior slightly to sync it up with Redux.
What two arguments does the Redux createStore command take?
the first if the rootReducer (imported from the reducers index) the second is the defaultState - which can just be an object literal declared within the statement, but it’s clearer to usually set it up as a separte variable and then just pass that variable as the argument.
What is going on in this statement below? export const history = syncHistoryWithStore(browserHistory, store);
We are taking the browser history and the push state, and then weaving in the actual store - we will need this history then to be accessible to other files because although we are creating it here, we will actually be using it elsewhere. We export it because that’s how you get something out of one file and into another.
What are Redux Actions?
Think of an Action as something that happens in your application - someone clicks on a photo, adds a comment, likes a picture or whatever. Whenever something happens, Redux dispatches an action.
What two things does a Redux dispatchAction contain?
- ) The type of action that happened (maybe ‘incrementLikes’ or ‘addComment’ or whatever)
- ) A payload of info that is needed - WHICH comment was deleted - who was the author - this is the info about the specifics of what happened.
Where do we create our Redux actions?
Within an ‘actions’ folder, under the client folder. Within this folder, we can either have one giant ‘actionCreators.js’ file - or we can break them off into individual files if we have a lot of them going on.
What does a Redux ActionCreator do?
It puts the action object together and dispatches it to our Reducer.
Within React, what actually updates our state?
Our Reducers handle and update the actual state.
What is a helpful way to think of actions?
As regular JS actions that just get fired off - but if no function is listening to that click or button or whatever - then nothing actually happens.
How many reducers do we create?
One for each piece of state - if you’re confused on what the pieces of state are, you can open up your store.js file and see. You need a reducer for each piece.
What two things does a reducer take in?
- The action (info about what happened)
2. A copy of current state.
How does a reducer work?
It takes in the store and info about what happened, then we do whatever work we need to do on it (update a number or whatever) - and then we return the new store, and then React/Redux takes over and does all it needs to do to update the UI and the Virtual DOM and so on for us.
Why do we pass an empty array for state as a default param to our Reducer functions?
Because when it initially runs, we won’t have state yet, so we need to account for that.
Why do we need rootReducer?
Because within Redux, ultimately we can only have one reducer - so, we create individual reducers for each piece of state, but then we need to put them into a rootReducer in order to use them.
Why do we need to pass rootReducer our routing ?
Because that is also going to live in our state - whenever we change the route - that lives within the browserHistory within state.
Where do we create our rootReducer?
Within an index.js file in our reducers folder. We have to import combineReducers from redux, and routerReducer from react-router-redux.
What is the react-redux package?
It provides the bindings for redux to react and lets us use them together. Though these two are often seen together - redux can actually be used with what you’d like to use it with.
When do we use curly brackets within an import?
For named exports - they are unneeded for default exports.