useReducer Flashcards

React hook

1
Q

What is useReducer?

A

useReducer is a React Hook that lets you add a “reducer” to your component.

const [state, dispatch] = useReducer(reducer, initialArg, init?)

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

Why use useReducer?

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

When use useReducer?

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

Where use useReducer?

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

How do useReducer work?

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

What is “reducer” in component concept?

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

How many parameters in useReducer?

A

Three parameters:
1/ reducer()
2/ initialArg
3/ init?

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

What is reducer() parameter?

A

The reducer function that specifies how the state gets updated.

The reducer() take state and action as arguments, and reducer() should return the next state.

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

What does useReducer return?

A

useReducer return an array has two values:

    1/ The current state. During the first render, it’s set to init(initialArg) or initialArg (if there’s no init).

    2/ The dispatch function that lets you update the state to a different value and trigger a re-render
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is “dispatch function”?

A

The “dispatch function” is returned by useReducer.

The “dispatch function” lets you update the state and trigger a re-render.

Need to pass the action as only argument to the dispatch function <=> The action is initiated through dispatch func.

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

What is the “action”?

A

The “action” is performed by user (pass through dispatch function).

An action is usually an object with a “type” property identifying it and other properties (optional).

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

Why does “dispatch function” exist?

A

I think the “action” is only passed to reducer() by dispatch function. And that is a reason =)))

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