Redux Flashcards

1
Q

What is the purpose of redux?

A

To manage state and to make state more predictable

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

What are the benefits of redux over native state management?

A

The data is separated from the UI and lives outside of the UI.

All of the data is located in one place and needs to be only changed once

This makes it easier to share data between components

Makes State more predictable

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

How does Redux store data?

A

In a state tree

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

what is the only thing that can change the state of a store?

A

Only an event can change the state of a store

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

What are “Actions”?

A

Actions are events that change the state of the store.

They are objects that contains a type, property and the state that needs to be mutated.

example:

{
    type:"ADD_GOAL",
    goal: {
        id: 0,
        name: "Run 5km"
     }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are action creators?

A

Action Creators are functions that create/return action objects

example:

const addItem = (item)=>{
    return {
        type:"ADD_ITEM",
        item
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Rules

A
  1. ONLY an event can change the state

2. The function that returns the new state needs to be a pure function

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

Pure Functions

A
  1. Always returns the same result if the same arguments are passed in
  2. Depends solely on the arguments passed into them
  3. Does not produce side effects (no interaction with the outside world)

// square() is a pure function

const square = x => x * x;

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

What is the main benefit of pure functions?

A

the most important feature of a pure function is that it’s predictable. If we have a function that takes in our state and an action that occurred, the function should (if it’s pure!) return the exact same result every single time.

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

What does the .dispatch method do?

A

it updates the state in the store by calling the reducer and then invoking any associated subscriptions.

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

Which three methods are available on the store?

A

.getState() -return state

.subscribe() -notifies when state changes

.dispatch(state, action) - updates the state

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

Which store method connects react components to the store?

A

.subscribe()

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

How to create a store with more than one reducer?

A

combine mutliple reducers into one root reducer like so:

function root (state = {},action){
    return{
        todos:todos(state.todo,action),
        goals:goals(state.goals,action)
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which function to call on the Redux library to combine multiple reducers?

A

Redux.combineReducers({
todos,
goals
}))

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

How to add middlewear into redux

A

const store = Redux.createStore( , Redux.applyMiddleware() )

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

How to save state to local storage in the browser?

A
const configureStore = () => {
  const store = createStore(Reducer);

store.subscribe(() => {
localStorage.state = JSON.stringify(store.getState());
});

return store;
};