Learn Redux - Wes Bos Flashcards

1
Q

Within React Router, what is an IndexRoute?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the main difference with Redux over plain React?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we see what is in our store (from within the console, say)

A

Provider.store.getState()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two core components behind working with Redux?

A

Reducers and Actions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where do we create our store.js file?

A

Within our client folder (not within the components folder)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the benefit of using syncHistoryWithStore?

A

We can hook up ReactRouter with our Redux instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why do we import browserHistory from React Router into our Redux Store instead of directly into our main app file?

A

Because we will need to modify its behavior slightly to sync it up with Redux.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What two arguments does the Redux createStore command take?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What is going on in this statement below?
export const history = syncHistoryWithStore(browserHistory, store);
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are Redux Actions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What two things does a Redux dispatchAction contain?

A
  1. ) The type of action that happened (maybe ‘incrementLikes’ or ‘addComment’ or whatever)
  2. ) 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Where do we create our Redux actions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does a Redux ActionCreator do?

A

It puts the action object together and dispatches it to our Reducer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Within React, what actually updates our state?

A

Our Reducers handle and update the actual state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a helpful way to think of actions?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many reducers do we create?

A

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.

17
Q

What two things does a reducer take in?

A
  1. The action (info about what happened)

2. A copy of current state.

18
Q

How does a reducer work?

A

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.

19
Q

Why do we pass an empty array for state as a default param to our Reducer functions?

A

Because when it initially runs, we won’t have state yet, so we need to account for that.

20
Q

Why do we need rootReducer?

A

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.

21
Q

Why do we need to pass rootReducer our routing ?

A

Because that is also going to live in our state - whenever we change the route - that lives within the browserHistory within state.

22
Q

Where do we create our rootReducer?

A

Within an index.js file in our reducers folder. We have to import combineReducers from redux, and routerReducer from react-router-redux.

23
Q

What is the react-redux package?

A

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.

24
Q

When do we use curly brackets within an import?

A

For named exports - they are unneeded for default exports.

25
Q

What is the Provider tag used for?

A

The Provider tag is used to wrap around the router in order to expose our store to our actual application. The Provider component takes a store prop - which we can import in from our store.js file.

26
Q

How do we hook up an Action and a Reducer?

A

We dispatch an action, and then the reducer will listen for it, and we write code within the reducer to handle it.

27
Q

When does a reducer run?

A

Every single reducer runs whenever any action is dispatched. We have to have logic within our reducer to determine whether or not to act on that particular action within our reducer. Otherwise, we need to just return state.

28
Q

How do we get state into Main with Redux?

A

Whereas in regular React we have to take the state at whatever component it lives and then pass it down and pass it down and pass it down every level until we get to where we want - with Redux we have access to the Connect function which allows us to inject that data at whatever level we actually need it.

29
Q

How do we expose our ActionCreators and our State to a component?

A

We create a second component that sort of sprinkles the ActionCreators and data on top using the maStateToProps and mapDispatchToProps functions.

30
Q

What do the mapStateToProps and mapDispatchToProps functions do?

A

They take our state and our dispatch and surface those data and functions via props into our component. via the components.

31
Q

What does calling your connect function against a component do?

A

It takes your regular component and adds all of the data from state to props, and adds all our Action Createors to props as well.

32
Q

When you use the React.cloneComponent method, what is one side benefit in regards to props?

A

It automatically passes down al the props to the nested child, so it has access to its parents props without having to specify them

33
Q

Why don’t you want to do something like

A

By using .bind(null, param) or in this case:

We pass null as the first argument because React already does all the binding for us.

34
Q

What’s important about the fact that Redux is using functional programming?

A

The golden rule of functional programming is that we do not mutate our state - we want all of our functions to be pure functions.

35
Q

What is a pure function?

A

A predictable function - every time we give it the same input, we expect to receive the same output.

36
Q

How should we handle changes to state within functional programming and Redux?

A

Take a copy of the state and modify that copy, then return the new state.

37
Q

If for some reason you do want to reference a DOM element directly from within direct (maybe to clear a form or get the value within the element) how do you do that?

A