Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React props syntax with ES6 spread operator [duplicate]

I noticed the following in a React app:

<UserList
  {...{ userIdsTyping, users }}
/>

What exactly is {...{ userIdsTyping, users }} doing here? I understand it's passing children to the UserList component, but how does the spread operator work here? What interaction does it have with userIdsTyping and users?

like image 621
randombits Avatar asked May 10 '26 19:05

randombits


1 Answers

The spread operator "expands" the object inline. It's equivalent to the following syntax:

<UserList
    userIdsTyping={userIdsTyping}
    users={users}
/>

The object itself { userIdsTyping, users } is ES6 shorthand, and expands to {userIdsTyping: userIdsTyping, users: users}.

This assumes values for userIdsTyping and users are defined somewhere else within scope.

like image 98
FeifanZ Avatar answered May 13 '26 09:05

FeifanZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!