Destructuring Flashcards

(9 cards)

1
Q

How is the spread operator (…) used in React props?

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

How do you destructure props in a function component’s

A

You can destructure props directly in the function parameters, like this: function MyComponent({ prop1, prop2 }) {…}.

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

Can you provide a default value while destructuring props?

A

Yes, you can provide default values during destructuring,such as { prop1 = ‘default’, prop2 }.

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

Is it necessary to destructure all props, or can you choose specific ones?

A

You can choose to destructure specific props based on your component’s needs, leaving others untouched.

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

Can you use the spread operator to combine props with additional ones?

A

Yes, you can combine existing props with additional ones using the spread operator, like <MyComponent {…props} newProp={value} />.

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

Does the spread operator create a shallow or deep copy of an object?

A

The spread operator creates a shallow copy of an object, meaning nested objects are still references to the original.

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

What is the purpose of the rest operator (…rest) in React?

A

The rest operator is used to collect remaining properties into a new object, often used in combination with props destructuring.

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

Why is array destructuring used for React Hooks like useState and object destructuring for props?

A

A React Hook like useState returns an array whereas props are an object; hence we need to apply the appropriate operation for the underlying data structure. The benefit of having an array returned from useState is that the values can be given any name in the destructuring operation.

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