Redux Basics Flashcards

1
Q

What is Redux approach on where to store the state of the application?

A

All state is store in a single state object, also called the state tree.

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

What is the first principle of Redux?

A

Everything that changes in the application, including data and UI state is stored in a single state object. The state tree.

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

What is the second principle of Redux?

A

The state tree is immutable. The only way to change it is by dispatching Actions.

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

Who can dispatch Actions?

A

Anyone that wants to change the app state: network events, user interactions on the UI, timers

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

What are Redux Actions?

A

They are plain Javascript objects which at a minimum say what change happened in the app. Ex:
{ type: “COMPLETE_TODO”, id: “1”}

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

What is the third principle of Redux?

A

To describe state mutation you have to write a pure function that takes the previous state of the application, the Action that will mutate it, and return the new state of the application. This is the reducer function.

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

Being pure, what are the constraints of the reducer function?

A
  • cannot create side effects
  • must depend only on the parameters given
  • must not change the parameters
  • must return a new state object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What should the reducer function do if it receives undefined as previous state?

A

It should return the application initial state.

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

Which concept from Redux joins the 3 principles together: state tree, action dispatch and reducer function?

A

The Store object

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

What are the main methods from the store object?

A

getState()
dispatch(action)
subscribe(callback)

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

How to integrate react with Redux?

A

On Redux subscriber you call react render. On react event callbacks you dispatch mutation events to Redux.

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

What is the JS ES5 function that makes an object immutable?

A

Object.freeze()

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

In Redux, what is the correct way of changing an array part of state?

A
By creating a copy of the array, using the concat(), slice() or the ... spread operator.
Ex: 
var a = [1,2,3]
return a.slice(1); // [2,3]
return [...a, 4] // [1,2,3,4]
return a.concat(4) // [1,2,3,4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a presentational component?

A

Component that only concerns in how it looks. Every UI action is received via props callbacks from above.

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

What are the pros and cons of presentational components?

A

Pro
- decouples most components from redux

Con
- requires callbacks to be passed down until leaves via props

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

What is a container component in Redux?

A

It is a component that calls the store object to get the state or mutate it.
It also subscribes to state changes and force rendering. on the componentDidMount() lifecycle method.

17
Q

Which method do you use to force a component rerendering?

A

forceUpdate()

18
Q

How do you use react-redux connect() to ease the creation of container components?

A
const visibleTodoList = connect(
  mapStateToProps,
  mapDispatchToProps)(TodoList);
const mapStateToProps = (state) =>  {
    return {
      todos: getVisibleTodos(
        state.todos,
        state.visibilityFilter)
    }
}
const mapDispatchToProps = (dispatch) =>  {
    return {
      onTodoClick: (id) => {
        dispatch({ type: "TOGGLE_TODO", id});
      }      
} }
19
Q

What are Action Generators? Why are they a good practice?

A

They are functions that create the actions that components can dispatch. They are good because it encapsulates and isolates the logic to generate actions, and it also helps keep the code documented.

20
Q

What library provides React binding to Redux?

A

react-redux

21
Q

What is react-redux Provider?

A

It is a component that aids in passing down the app state implicitly via React’s Context API

22
Q

How would you wrap a component with the Provider from react-redux?

A
23
Q

What does the mapStateToProps function that should be passed to connect() do?

A

It receives the state object and generates a props object that should be passed to the component connect() will create.

24
Q

What does the mapDispatchToProps function that should be passed to connect() do?

A

It receives the dispatch function from Redux and returns a map of callbacks that can be used by the component connect() will create.

25
Q

How would you simplify the following arrow function in ES6?

const addTodo = (text) => {
  return {
    type: "ADD_TODO",
    text: text,
  };
};
A
const addTodo = (text) => ({
    type: "ADD_TODO",
    text,
  });
26
Q

How would you simplify the following arrow function in ES6?

const mapDispatchToProps = (dispatch) => ({
    onTodoClick: (id) => {
      dispatch(toggleTodo(id));
    },
});
A
const mapDispatchToProps = (dispatch) => ({
    onTodoClick(id) {
      dispatch(toggleTodo(id));
    },
});
27
Q

What does the combineReducer() helper function from Redux do?

A

Give two or more reducer, it returns a new reducer that is the composition of the given reducers.

28
Q

Write an example of usage of combineReducer()

A

const rootReducer = combineReducers({ counter, todos })

29
Q

Given the reducer from combineReducer({ counter, todo }), how will the state object store the state return by each reducer? How does redux call each part of that state?

A
In an object with properties with the name of each reducer:
{ 
  counter: COUNTER_STATE,
  todo: TODO_STATE,
}

Redux call each part of the state the “slice of the state”

30
Q

How can I pass an object called “initialState” when I initialize redux store?

A

createStore(rootReducer, initialState)