3

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?

FeifanZ
  • 16,250
  • 7
  • 45
  • 84
randombits
  • 47,058
  • 76
  • 251
  • 433

1 Answers1

7

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.

FeifanZ
  • 16,250
  • 7
  • 45
  • 84