Redux Flashcards

1
Q

Container

A
  • A container does data fetching and then renders its corresponding sub-component. That’s it.
  • I call containers React components that are aware of Redux, Router, etc. They are more coupled to the app. Same as “smart components”.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Component (vs Container)

A

I call components encapsulated React components that are driven solely by props and don’t talk to Redux. Same as “dumb components”. They should stay the same regardless of your router, data fetching library, etc.

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

Why the spread operator

A

Since one of the core tenets of Redux is to never mutate state, you’ll often find yourself using Object.assign() to create copies of objects with new or updated values. While effective, using Object.assign() can quickly make simple reducers difficult to read given its rather verbose syntax.

An alternative approach is to use the object spread syntax proposed for the next versions of JavaScript which lets you use the spread (…) operator to copy enumerable properties from one object to another in a more succinct way. The object spread operator is conceptually similar to the ES6 array spread operator.

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

Subscribe

A

Subscribing to the store

```javascript
store.subscribe(() => {
// This is called every time anything changes
let currentState = store.getState();
});
~~~

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

Reducer

A

The root piece of the app A function that takes 2 args: the current state, and the action Return the new state

```javascript
const initialState = [];
function reducer(state = initialState, action) {
switch (action.type) {
case LOG_EVENT:
return [action.payload, …state];
}
return state;
}
~~~

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

Action

A

Representation of what you’re trying to do

```javascript
{
type: ‘SOME_VALUE’ // Required, usually a string
payload: {} // Convention
}
~~~

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

Hot reloading

A
  • Quand on met à jour un component, l’appli se recharge toute seule
  • Quand on met à jour un reducer, il faut un full reload (par défaut)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How reducers work

A

Everytime you dispatch an action, every single reducer in going to run. Whether you choose to act on that is up to the reducer (ndlr: hence the switch statement)

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

Actions and Reducers

A
  • Une action est comme un event

- Un reducer est comme un event handler

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

Main ideas about Redux

A
  • There is a single source of truth
  • Just one state object
  • The flow of data is unidirectional
  • When you dispatch an action, it goes to the store
  • The state is read only
  • Only getter
  • To “set”, you dispatch an action
  • It enforces you immutability
  • Don’t modify objects, create new copy of objects (efficiently)
  • Careful: Nothing prevents you to do mutations
  • It’s composable
  • “It’s all just functions”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Store

A

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. To create it, pass your root reducing function to createStore.

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

dispatch(action)

A

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

The store’s reducing function will be called with the current getState() result and the given action synchronously. Its return value will be considered the next state. It will be returned from getState() from now on, and the change listeners will immediately be notified

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

subscribe(listener)

A

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.

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

getState()

A

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

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

connect()

A

Connects a React component to a Redux store

```javascript
connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
~~~

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

mapStateToProps

A

If specified, the component will subscribe to Redux store updates. Any time it updates, mapStateToProps will be called. Its result must be a plain object, and it will be merged into the component’s props. If you omit it, the component will not be subscribed to the Redux store. If ownProps is specified as a second argument, its value will be the props passed to your component, and mapStateToProps will be re-invoked whenever the component receives new props.

```javascript
[mapStateToProps(state, [ownProps]): stateProps] (Function)
~~~

17
Q

Do I have to use the switch statement to handle actions?

A

No. You are welcome to use any approach you’d like to respond to an action in a reducer. The switch statement is the most common approach, but it’s fine to use if statements, a lookup table of functions, or to create a function that abstracts this away.

18
Q

One store

A
  • A store is an object that holds the application’s state tree.
  • There should only be a single store in a Redux app, as the composition happens on the reducer level.