8

I know that a React Fragment can be used to group child components like this:

render() {
  return (
    <React.Fragment>
      <Foo />
      <Bar />
      <Baz />
    </React.Fragment>
  );
}

But can you add props to it? Say you want to use the spread operator:

<React.Fragment {...otherProps}>
  ...
</React.Fragment>
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114

2 Answers2

14

No, you can't. As per the React docs on Fragments:

key is the only attribute that can be passed to Fragment. In the future, we may add support for additional attributes, such as event handlers.

Therefore, if you want to add props to a wrapper component, switch to a good ol' div.

Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
0

Not at all because if you think to pass props like this -

<React.Fragment {...otherProps}>
....
<React.Fragment />

it means

< {...otherProps}>
</>

Which isn't right that Reactjs doesn't recommend to use like this.

akhtarvahid
  • 9,445
  • 2
  • 26
  • 29