Redux Fundamentals Flashcards

1
Q

Action

A

An action is a simple Javascript object. It contains the updated data and the type of operation that needs to be performed to mutate the store. The action forwars this information to the dispatcher

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

Dispatcher

A

Dispatch the appropriate callback functions that the store registers. It also maintains the dependency between multiple stores in the application

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

Store

A

The store contains all the logic used to mutate the state.

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

Redux architecture

unidirectional data flow

A
  1. Action
  2. reducer
  3. store
  4. view
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Three principles of Redux

A
  1. A Redux app should have a single source of truth called the store
  2. The store is read-only and is mutated soley by displatching actions
  3. Changes to the store are made using pure functions called reducers

First principle

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

Second Principle

A

An action contains information about how to modify the state. The action forwards the information to a reducer function

var action = {}
action = {
    type: 'CHANGE_EMAIL',
    payload: 'johndoe@email.com'
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Third Principle

A

Reducers mutate the state. The reducer function takes two arguments – state and action. It uses the information inside the action to mutate the state. Since the view/component has already been subscribed to the store, it gets updated with the new data automatically.

// Redux Store
let store = {
    name: 'John Doe',
    email: 'john@doe.com'
}

// Redux Action
let action = {
    type: 'CHANGE_EMAIL',
    payload: 'johndoe@email.com'
}

// Redux Reducer function
let reducer = (store, action) => {
    switch (action.type) {
        case 'CHANGE_EMAIL':
            let newState = {
                ...store,
                email: action.payload
            }

            return newState;

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

Unidirectional dataflow of Redux architecture

A
  1. user event
  2. action forwards this to reducer
  3. reducer takes the current state from the store
  4. reducer mutates the state with the action’s payload
  5. view subscibes to the store and displays the new state
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Three types of state

A
  1. global
  2. multicomponent
  3. local
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Global state

A

stores the data this is accessible from anywhere in an application and can be accessed by all components

let user = {
  name: 'John Doe'
}

let profileComponent = () => {
  const userProfile = {
    name: user.name
  }

  console.log('Profile Component:');
  console.log(userProfile);
}

let checkoutComponent = () => {
  const userAddress = {
    name: user.name
  }

  console.log('Checkout Component:')
  console.log(userAddress);
}

let shoppingCartComponent = () => {
  const cart = {
    user: user.name,
    products: [
      // ...
    ]
  }

  console.log('Shopping Cart Component:')
  console.log(cart);
}

profileComponent();
checkoutComponent();
shoppingCartComponent();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Multicomponent state

A

state is shared among various application components

let user = {
  name: 'John Doe',
  email: 'johndoe@gmail.com'
}

let profileComponent = () => {
  const userProfile = {
    name: user.name,
    email: user.email
  }

  console.log('Profile Component:');
  console.log(userProfile);
}

let checkoutComponent = () => {
  const userAddress = {
    name: user.name,
    email: user.email
  }

  console.log('Checkout Component:')
  console.log(userAddress);
}

let shoppingCartComponent = () => {
  const cart = {
    user: user.name,
    products: [
      // ...
    ]
  }

  console.log('Shopping Cart Component:')
  console.log(cart);
}

profileComponent();
checkoutComponent();
shoppingCartComponent();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Local state

A

Data stored in an individual component

let user = {
  name: 'John Doe',
  email: 'johndoe@gmail.com'
}

let profileComponent = () => {
  const userProfile = {
    name: user.name,
    email: user.email
  }

  console.log('Profile Component:');
  console.log(userProfile);
}

let checkoutComponent = () => {
  const userAddress = {
    name: user.name,
    email: user.email
  }

  console.log('Checkout Component:')
  console.log(userAddress);
}

let shoppingCartComponent = () => {
  const cart = {
    user: user.name,
    products: [
      // ...
    ]
  }

  console.log('Shopping Cart Component:')
  console.log(cart);
}

profileComponent();
checkoutComponent();
shoppingCartComponent();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Redux store

A

a central, immutable data container that holds all the information about an application’s state. The store is simply a JavaScript object

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

How does Redux-based data sharing differ from service-based data sharing?

A

In redux all of the application subscribes to the store instead of individual services

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

Store organization

A
  1. global states - accessible from anywhere in an application and can be accessed by all components. Therefore, we should keep global states in a store
  2. multicomponent states - state is shared among various application components. Ok to keep state inside of store but if components have a parent-child relationship. we should use prop-lifting or prop-drilling instead
  3. local states should not be kept inside the store
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does redux decrease HTTP requests

A

After making the HTTP request we can put the data in the Redux store and retrieve it afterward

17
Q

List typical types of non-serializable data

A
  1. data that is too large to be stored in a database
  2. data that needs top be processed before it can be stored
  3. data with many changes over time, like live video or social media feeds
  4. Javascript Promises, symbols. Maps and Sets, function and class instances
18
Q

Characteristics of reducers

A
  1. pure functions
  2. don’t create side effects
  3. reducer functions receive some data as arguments and perform an immutable operation on them
19
Q

pure functions

A

pure functions always return the same output for the same given arguments

20
Q

impure function

A

impure functions generate outputs for the same inputs

21
Q

side effects

A

impure functions create side effects. Side effects are any chnages to the state that happen from outside the scope of the function

22
Q

Reducers in Redux

A

pure functions that mutate states according to the action’s type

23
Q

Benefits of Redux

A
  1. centralized store
  2. page speed optimization
  3. debugging
  4. unit testing
  5. component communication
  6. performance improvement
24
Q

Centralized store benefit

A

every time the store is updated all the parts are updated accordingly. This makes it easier to pinpoint an issue

25
Q

How does redux help page speed optimization

A

Redux helps implement the OnPush change detection strategy. Redux notifies Angular of the chnage so that Angular can update only the affected parts of the page. This approach eliminates the need for costly rebuilds.

26
Q

How does Redux help debug an Angular application

A

redux provides a history of state changes making it possible to visualize the state tree, time travel debugging, record and replace debugging, and hot reloading

27
Q

state tree

A

hierarchical representation of the application’s state at any given point in time.

28
Q

record and replay

A

lets us record or replay all or part of an applications execution