How is the spread operator (…) used in React props?
How do you destructure props in a function component’s
You can destructure props directly in the function parameters, like this: function MyComponent({ prop1, prop2 }) {…}.
Can you provide a default value while destructuring props?
Yes, you can provide default values during destructuring,such as { prop1 = ‘default’, prop2 }.
Is it necessary to destructure all props, or can you choose specific ones?
You can choose to destructure specific props based on your component’s needs, leaving others untouched.
Can you use the spread operator to combine props with additional ones?
Yes, you can combine existing props with additional ones using the spread operator, like <MyComponent {…props} newProp={value} />.
Does the spread operator create a shallow or deep copy of an object?
The spread operator creates a shallow copy of an object, meaning nested objects are still references to the original.
What is the purpose of the rest operator (…rest) in React?
The rest operator is used to collect remaining properties into a new object, often used in combination with props destructuring.
Why is array destructuring used for React Hooks like useState and object destructuring for props?
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.