w7d1 - redux Flashcards

1
Q

Three principles of Redux

A

Single Source of Truth

State is Read-Only

Only Pure Functions Change State

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

What does Single Source of Truth mean?

A

The entire state of the application is stored in a single JS object in a single store, commonly referred to as a ‘state tree’.

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

What does State is Read-Only mean?

A

The only way to change the state is to dispatch an action. This principle ensures that our Redux loop is never short-circuited and change of state remains single-threaded.

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

What is the Redux loop?

A

Action -> Dispatcher -> Store -> View

Optionally a View may create an Action that loops back to the Dispatcher, Store and View in that order.

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

What does the Store do?

A

It holds the global state of an application.

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

How do you create a Store in redux?

A

createStore(reducer, [preLoadedState], [enhancer])

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

What does the reducer do?

A

The reducer is a reducing function that receives the app’s current state and incoming actions, determines how to update the store’s state, and returns the next state

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

What does the preloadedState argument for createStore do?

A

It’s an optional object representing any application state that existed before the state was created.

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

What does the preLoadedState argument for createStore do?

A

It’s an optional function that adds extra functionality to the store

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

How do you get the current store’s state?

A

store.getState( )

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

How do you pass an action into the store’s reducer?

A

dispatch( action )

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

How do you register a callback to be triggered whenever the store updates?

A

subscribe( callback )

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

What’s unique about callbacks registered to the store?

A

Once they’re called, they’re unsubscribed, and so they will only be called once.

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

What exactly is a redux action?

A

a POJO with a mandatory ‘type’ key and optional payload keys that contain new information to be merged into the store.

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

How do you set up a basic redux action containing an orange with type ADD_FRUIT?

A
const addOrange = {
 type: "ADD_FRUIT",
  fruit: "orange"
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Name of the anti-pattern that involves passing references down to deeply-nested components.

Why is it bad?

A

prop-threading

Tedious and error-prone.

17
Q

How is prop-threading corrected?

A

By setting up a Provider component that exists solely to provide global-ish access to the store to all child components.

18
Q

What does connect( ) do?

A

It’s a higher-order function that ____

19
Q

What does Object.assign do?

A

It merges (via shallow-copy) an arbitrary number of args, to the left.

20
Q

What’s the big-O of a shallow-copy?

A

O(1). It effectively passes a reference.

21
Q

When do you invoke a reducer by hand?

A

Never. They are always invoked by the dispatcher.

22
Q

What does mapStateToProps do?

A

It literally maps some keys of state into props to be passed to a Component. Specifically, the props that the Component actually cares about.

23
Q

How do you indicate to a Component to access state from a Provider?

A

<Provider>
<ComponenentThatCares></ComponenentThatCares>
</Provider>

24
Q

How does <form onsubmit="..."> differ from using a button to submit the form?

A

With onsubmit, pressing enter will submit the form.

25
Q

Why use onChange on an input in a form?

A

To continuously update the virtual DOM

26
Q

What does combineReducers do? Take as args?

A

It will iterate over all reducers passed in, combine the resulting states, and return a new state.

27
Q

Do you need to bind a method that does not make use of ‘this’ ?

A

Nope.

28
Q

What are the arguments passed in to connect( ) ?

A

connect( mapStatesToProps, mapDispatchToProps )

29
Q

What does the mapStatesToPro argument of connect( ) do? What is it?

A

A function that tells connect( ) how to map the state into your component’s props.

It takes these arguments:

mapStateToProps( state, [ownProps])