Redux Flashcards

1
Q

What are the Three Principles of Redux?

A
  1. Single source of truth - The global state of your application is stored in an object tree within a single store
  2. State is read-only - The only way to change the state is to emit an action, an object describing what happened
  3. Changes are made with pure functions - To specify how the state tree is transformed by actions, you write pure reducers.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an action creator?

A

A factory function that takes data and returns an action to be dispatched to the store.

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

Why use action creators?

A
  • Single source of truth for the action creation process
  • Separates the logic for the action from the dispatch caller.
  • Isolate views and unit tests from non-deterministic processes like creating timestamps and random numbers.
  • Good place for the canonical source of truth for the action type (only the action creator needs to know the action type)
  • Lint tools and IDE will help protect you from types trying to get at the action type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why are state transactions useful?

A
  1. Easy testability
  2. Easy undo/redo
  3. Time travel debugging
  4. Durability; even if the state gets wiped out, you can use the transactions to reproduce it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When might Redux be beneficial to your app?

A
  1. User workflows are complex
  2. Your app has a large variety of user workflows (e.g. regular vs admin)
  3. Users can collaborate
  4. You’re using web sockets or server-sent events
  5. You’re loading data from multiple endpoints to build a single view
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why use Redux?

A
  1. Enable deterministic state reproduction
  2. …which helps give you deterministic view renders
  3. Enable easy undo/redo features
  4. Enable time travel debugging
  5. Decouples the app into different buckets - presentation, state logic, and effects. Helps us obey the single responsibility principle.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some things to keep in mind when using Redux?

A
  1. Some apps don’t need Redux
  2. Use constants for action types
  3. Use action creators to decouple action logic from dispatch callers
  4. Use ES6 parameter defaults for self-describing signatures
  5. Use selectors for calculated state and decoupling
  6. Always use TDD
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a selector?

A

A function used to select state from the store. A view should only read the state via selectors.

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

Why use selectors?

A
  • Eliminate state shape dependencies - When a function knows too much about its argument’s shape, if state shape changes, you have to change all the code that was dependent on the previous shape!
    (A small change in requirements should lead to only a small change in implementation.)
  • Make dependent state logic easier by giving it a single, well-known place to live.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does it mean for Redux to be the single source of truth?

A
  • For every piece of state, there should be one single source of truth for that state.
  • For shared application state, all of that can be managed in one store.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a reducer?

A
  • The ONLY thing that updates the state
  • Pure function which takes (state, action) => new state
  • Pure because your state updates should be deterministic, and purity guarantees that
How well did you know this?
1
Not at all
2
3
4
5
Perfectly