State management Flashcards

1
Q

What is the primary purpose of useContext in React?

A

To allow components to access context values without needing to pass props down manually at every level.

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

True or False: useContext can only be used with functional components.

A

True

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

Fill in the blank: useContext takes a single argument, which is the ________ created by React.createContext().

A

context

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

Multiple choice: Which of the following hooks is commonly used alongside useContext for state management?
A) useState

B) useReducer

C) Both A and B

A

C) Both A and B

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

What is the return value of useContext?

A

The current value of the context that is provided to the nearest matching Provider.

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

What is the primary purpose of the useReducer hook in React?

A

To manage complex state logic in functional components.

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

True or False: useReducer can replace useState for state management in React.

A

True

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

Fill in the blank: useReducer takes two parameters: a reducer function and _____.

A

an initial state

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

Multiple choice: Which of the following is a valid signature for the useReducer hook?
A) useReducer(reducer, initialState)

B) useReducer(initialState, reducer)

C) useReducer(reducer, initialState, initializer)

A

A and C

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

What does the reducer function receive as arguments in useReducer?

A

The current state and an action object.

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

What is the primary purpose of the useState hook in React?

A

To add state management to functional components.

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

True or False: useState can only be used in class components.

A

False

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

Fill in the blank: The useState hook returns an array with __________.

A

two elements: the current state and a function to update it.

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

Which syntax is correct for initializing state using useState?

A

const [state, setState] = useState(initialValue);

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

What type of values can be stored in a state variable created by useState?

A

Any type of value, including numbers, strings, objects, and arrays.

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