Basic Tutorial Flashcards

1
Q

What are actions?

A

Actions are plain JavaScript objects or payloads of information that send data from your application to your store.

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

What is the only source of information for the store?

A

Actions.

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

How do you send an action to the store?

A

By using using store.dispatch().

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

What must actions have?

A

A type property that indicates the type of action being performed.

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

What should types typically be defined as?

A

String constants.

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

What is this an example of?

const ADD_TODO = ‘ADD_TODO’

{
type: ADD_TODO,
text: ‘Build my first Redux app’
}

A

An action which represents adding a new todo item.

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

What might you want to do once your app is large enough?

A

Move Actions into a separate module.

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

It’s a good idea to pass as little data in each action as possible.

A

For example, it’s better to pass index than the whole todo object.

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

What are action creators?

A

Functions that create actions.

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

Why would you use an action creator to return an action?

A

To make them portable and easy to test.

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

What is an example of an action creator?

A
function addTodo(text) {
  return {
    type: ADD_TODO,
    text
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you initiate a dispatch in Redux?

A

Pass the result to the dispatch() function, or create a bound action creator that automatically dispatches.

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

Give a simple example of passing a result to a dispatch() function.

A

dispatch(addTodo(text))

dispatch(completeTodo(index))

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

Give a simple example of how you would create a bound action creator that automatically dispatches.

A
const boundAddTodo = text => dispatch(addTodo(text))
const boundCompleteTodo = index => dispatch(completeTodo(index))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the advantage of using a bound action creator that automatically dispatches?

A

You can call them directly.

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

Give a simple example of how you would directly call a bound action creator that automatically dispatches.

A

boundAddTodo(text)

boundCompleteTodo(index)

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

How would you directly access the the dispatch() function from the store?

A

by using store.dispatch() or more likely by accessing it using a helper like react-redux’s connect().

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

How would you automatically bind many action creators to a dispatch() function?

A

By using bindActionCreators()

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

What do reducers do?

A

They specify how the application’s state changes in response to actions sent to the store.

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

How do actions differ from reducers?

A

Actions only describe what happened, but don’t describe how the application’s state changes.

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

How is all the applications state stored?

A

In a single object.

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

What is a good practice before creating an apps state?

A

To figure out the minimal representation of your app’s state as an object.

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

what should you do if you need to store some data, as well as some UI state, in the state tree?

A

Try to keep them separate.

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

What’s a best practice when creating an apps state?

A

To keep it as normalized as possible, without any nesting and to keep every entity in an object stored with an ID as a key, and use IDs to reference it from other entities, or lists.

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

What is a reducer?

A

A pure function that takes the previous state and an action, and returns the next state.

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

Give an example of a reducer.

A

(previousState, action) => nextState

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

Why is it called a reducer?

A

Because it’s the type of function you would pass to Array.prototype.reduce(reducer, ?initialValue).

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

What should you never do in a reducer?

A

Mutate its arguments
Perform side effects like API calls and routing transitions
Call non-pure functions, e.g. Date.now() or Math.random()

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

What should a reducer do?

A

Given the same arguments, it should calculate the next state and return it. No surprises. No side effects. No API calls. No mutations. Just a calculation.

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

What will Redux do the first time?

A

Call our reducer with an undefined state

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

When Redux calls our reducer for the first time, what is this a good opportunity to do?

A

To return the initial state of our app.

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

Convert this code to take advantage of ES6
default argument syntax:

import { VisibilityFilters } from './actions'
const initialState = {
  visibilityFilter: VisibilityFilters.SHOW_ALL,
  todos: []
}
function todoApp(state, action) {
  if (typeof state === 'undefined') {
    return initialState
  }
  // For now, don't handle any actions
  // and just return the state given to us.
  return state
}
A
import { VisibilityFilters } from './actions'
const initialState = {
  visibilityFilter: VisibilityFilters.SHOW_ALL,
  todos: []
}
function todoApp(state = initialState, action) {
  // For now, don't handle any actions
  // and just return the state given to us.
  return state
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What do you notice with this code?

import {
  SET_VISIBILITY_FILTER,
  VisibilityFilters
} from './actions'
...
function todoApp(state = initialState, action) {
  switch (action.type) {
    case SET_VISIBILITY_FILTER:
      return Object.assign({}, state, {
        visibilityFilter: action.filter
      })
    default:
      return state
  }
}
A

We don’t mutate the state. We create a copy with Object.assign().
NOTE:
Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter.

We return the previous state in the default case. It’s important to return the previous state for any unknown action.

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

What is noteworthy about Object.assign?

A

It’s part of ES6, and is not supported by older browsers. To support them, you will need to either use a polyfill, a Babel plugin, or a helper from another library like _.assign().

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

How would you update a specific item in an array without resorting to mutations?

A

Create a new array with the same items except the item at the index.

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

If you find yourself often wanting to update a specific item in an array without resorting to mutations, what is it a good idea to do?

A

Use a helper like immutability-helper, updeep, or even a library like Immutable that has native support for deep updates.

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

When is the only time you can assign to anything inside the state?

A

When you clone it first.

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

What do you notice here?

function todos(state = [], action) {
  switch (action.type) {
    case ADD_TODO:
      return [
        ...state,
        {
          text: action.text,
          completed: false
        }
      ]
    case TOGGLE_TODO:
      return state.map((todo, index) => {
        if (index === action.index) {
          return Object.assign({}, todo, {
            completed: !todo.completed
          })
        }
        return todo
      })
    default:
      return state
  }
}
function todoApp(state = initialState, action) {
  switch (action.type) {
    case SET_VISIBILITY_FILTER:
      return Object.assign({}, state, {
        visibilityFilter: action.filter
      })
    case ADD_TODO:
      return Object.assign({}, state, {
        todos: todos(state.todos, action)
      })
    case TOGGLE_TODO:
      return Object.assign({}, state, {
        todos: todos(state.todos, action)
      })
    default:
      return state
  }
}
A

todos also accepts state—but state is an array! Now todoApp gives todos just a slice of the state to manage, and todos knows how to update just that slice. This is called reducer composition, and it’s the fundamental pattern of building Redux apps.

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

reducer1

A

qw

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

reducer2

A

qw

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

reducer3

A

sd

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

reducer4

A

qq

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

reducer5

A

11

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

reducer6

A

qq

45
Q

reducer7

A

qq

46
Q

reducer8

A

qq

47
Q

reducer9

A

qq

48
Q

reducer10

A

qq

49
Q

reducer11

A

qq

50
Q

reducer12

A

qq

51
Q

reducer13

A

qq

52
Q

reducer14

A

qq

53
Q

reducer15

A

qq

54
Q

What is the store?

A

It’s an object that brings actions and reducers together.

55
Q

What is the stores responsibilities?

A

Holds application state;
Allows access to state via getState();
Allows state to be updated via dispatch(action);
Registers listeners via subscribe(listener);
Handles unregistering of listeners via the function returned by subscribe(listener).

56
Q

How many stores will you have in a Redux application?

A

One.

57
Q

What would you do if you want to split your data handling logic?

A

Use reducer composition.

58
Q

How would you create a store from a reducer?

A
import { createStore } from 'redux'
import todoApp from './reducers'
const store = createStore(todoApp)
59
Q

What may you do as the second argument to createStore()?

A

Specify the initial state.

60
Q

Why is it useful to optionally specify the initial state as the second argument to createStore()?

A

For hydrating the state of the client to match the state of a Redux application running on the server.

61
Q

Give an example of using the second argument to createStore().

A

const store = createStore(todoApp, window.STATE_FROM_SERVER)

62
Q

What does the Redux architecture revolve around?

A

A strict unidirectional data flow.

63
Q

What does it mean when the Redux architecture is said to revolve around a strict unidirectional data flow?

A

all data in an application follows the same lifecycle pattern, making the logic of your app more predictable and easier to understand.

64
Q

What does the Redux architecture revolving around a strict unidirectional data flow encourage and why is it desirable?

A

Data normalization, so that you don’t end up with multiple, independent copies of the same data that are unaware of one another.

65
Q

What are the data lifecycle steps in any Redux app?

A

You call store.dispatch(action)
The Redux store calls the reducer function you gave it.
The root reducer may combine the output of multiple reducers into a single state tree.
The Redux store saves the complete state tree returned by the root reducer.

66
Q

What is this an example of?

{ type: ‘LIKE_ARTICLE’, articleId: 42 }
{ type: ‘FETCH_USER_SUCCESS’, response: { id: 3, name: ‘Mary’ } }
{ type: ‘ADD_TODO’, text: ‘Read the Redux docs.’ }

A

An action.

67
Q

What’s a good way of thinking about Actions? Give some examples.

A

As a very brief snippet of news.

“Mary liked article 42.” or “’Read the Redux docs.’ was added to the list of todos.”

68
Q

Where can you call store.dispatch(action)?

A

From anywhere in your app, including components and XHR callbacks, or even at scheduled intervals.

69
Q

What arguments will the store pass to the reducer?

A

The current state tree and the action.

70
Q

What is this an example of?

// The current application state (list of todos and chosen filter)
let previousState = {
  visibleTodoFilter: 'SHOW_ALL',
  todos: [
    {
      text: 'Read the docs.',
      complete: false
    }
  ]
}
// The action being performed (adding a todo)
let action = {
  type: 'ADD_TODO',
  text: 'Understand the flow.'
}
// Your reducer returns the next application state
let nextState = todoApp(previousState, action)
A

Something the root reducer might receive.

71
Q

When should you perform any side effects like API calls or router transitions.

A

Before an action is dispatched.

72
Q

What is the combineReducers() helper function useful for?

A

“splitting” the root reducer into separate functions that each manage one branch of the state tree.

73
Q

Give and example of how combineReducers() work?

A

Let’s say you have two reducers, one for a list of todos, and another for the currently selected filter setting. When you emit an action, todoApp returned by combineReducers will call both reducers. It will then combine both sets of results into a single state tree.

74
Q

Give and example of how combineReducers() work using two reducers, one for a list of todos, and another for the currently selected filter setting.

function todos(state = [], action) {
  // Somehow calculate it...
  return nextState
}
function visibleTodoFilter(state = 'SHOW_ALL', action) {
  // Somehow calculate it...
  return nextState
}
let todoApp = combineReducers({
  todos,
  visibleTodoFilter
})
A

When you emit an action, todoApp returned by combineReducers will call both reducers.

let nextTodos = todos(state.todos, action)
let nextVisibleTodoFilter = visibleTodoFilter(state.visibleTodoFilter, action)

It will then combine both sets of results into a single state tree:

return {
todos: nextTodos,
visibleTodoFilter: nextVisibleTodoFilter
}

75
Q

What happens when the Redux store saves the complete state tree returned by the root reducer.

A

This new tree is now the next state of your app.

76
Q

When the Redux store saves the complete state tree returned by the root reducer, what happens to every listener registered with store.subscribe(listener).

A

They will now be invoked.

77
Q

Bottom of Data Flow

A

11

78
Q

Bottom of Data Flow2

A

11

79
Q

Why does Redux work especially well with libraries like React and Deku?

A

Because they let you describe UI as a function of state, and Redux emits state updates in response to actions.

80
Q

React bindings are not included in Redux by default. How would you install them explicitly?

A

npm install –save react-redux

81
Q

What is the function of React bindings for Redux?

A

To separate presentational components from container components.

82
Q

Why is it a good idea to separate presentational components from container components?

A

Can make your app easier to understand and allow you to more easily reuse components.

83
Q

What is the purpose of presentational components?

A

To define how things look (markup, styles).

84
Q

What is the purpose of container components?

A

To define how things work (data fetching, state updates).

85
Q

Are presentational components aware of Redux?

A

No.

86
Q

Are container components aware of Redux?

A

Yes.

87
Q

How does presentational components read data?

A

From props.

88
Q

How does container components read data?

A

By subscribing to Redux state.

89
Q

How does presentational components change data?

A

By invoking callbacks from props.

90
Q

How does container components change data?

A

By dispatching Redux actions.

91
Q

How are presentational components written?

A

Manually.

92
Q

How are container components written?

A

Usually generated by React Redux.

93
Q

What should you do if a container component becomes too complex (i.e. it has heavily nested presentational components with countless callbacks being passed down)?

A

Introduce another container within the component tree.

94
Q

Why shouldn’t you write the container components by hand using store.subscribe()?

A

Because React Redux makes many performance optimizations that are hard to do by hand. For this reason, rather than write container components, we will generate them using the connect() function provided by React Redux.

95
Q

What should you do if it’s hard to tell if some component should be a presentational component or a container?

A

Just mix the presentation and logic in a component. As it grows, it will be more obvious how to split it.

96
Q

What is a container component?

A

It’s just a React component that uses store.subscribe() to read a part of the Redux state tree and supply props to a presentational component it renders.

97
Q

What is a better practice than writing a container component by hand?

A

Generating container components with the React Redux library’s connect() function.

98
Q

Why is it a better practice to generate container components with the React Redux library’s connect() function than writing a container component by hand?

A

It provides many useful optimizations to prevent unnecessary re-renders. (One result of this is that you shouldn’t have to worry about the React performance suggestion of implementing shouldComponentUpdate yourself.)

99
Q

How would you use connect()

A

Define a special function called mapStateToProps that describes how to transform the current Redux store state into the props you want to pass to a presentational component you are wrapping.

100
Q

What can container components do apart from reading the state?

A

Dispatch actions.

101
Q

What is the user defined function called mapDispatchToProps() used for?

A

To receive the dispatch() method and return callback props that you want to inject into the presentational component.

102
Q

Define a function that filters the state.todos according to the state.visibilityFilter, and use it in its mapStateToProps.

A
const getVisibleTodos = (todos, filter) => {
  switch (filter) {
    case 'SHOW_COMPLETED':
      return todos.filter(t => t.completed)
    case 'SHOW_ACTIVE':
      return todos.filter(t => !t.completed)
    case 'SHOW_ALL':
    default:
      return todos
  }
}
const mapStateToProps = state => {
  return {
    todos: getVisibleTodos(state.todos, state.visibilityFilter)
  }
}
103
Q

Create a function where VisibleTodoList injects a prop called onTodoClick into the TodoList component, and for onTodoClick to dispatch a TOGGLE_TODO action.

A
const mapDispatchToProps = dispatch => {
  return {
    onTodoClick: id => {
      dispatch(toggleTodo(id))
    }
  }
}
104
Q

Create VisibleTodoList by calling connect() and passing these two functions: mapStateToProps and mapDispatchToProps.

A
import { connect } from 'react-redux'
const VisibleTodoList = connect(
  mapStateToProps,
  mapDispatchToProps
)(TodoList)
export default VisibleTodoList
105
Q

Why do all container components need access to the Redux store?

A

So they can subscribe to it.

106
Q

Why wouldn’t you pass the store as a prop to every container component?

A

Because it gets tedious, as you have to wire store even through presentational components just because they happen to render a container deep in the component tree.

107
Q

Whats a better option than passing the store as a prop to every container component?

A

Use a special React Redux component called < Provider > to magically make the store available to all container components in the application without passing it explicitly.

108
Q

How many times do you need to use < Provider >?

A

Once.

109
Q

When do you need to use < Provider >?

A

when you render the root component.