你可以在 React.Fragment 上使用道具吗?

Can you use props on React.Fragment?

我知道 React Fragment 可以像这样对子组件进行分组:

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

但是能给它加上道具吗?假设您想使用展开运算符:

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

不,你不能。根据 Fragments 上的 React docs

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.

因此,如果你想将 props 添加到包装器组件,请切换到一个好的 ol' div。

一点也不,因为如果你想像这样传递道具 -

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

意思是

< {...otherProps}>
</>

Reactjs 不建议这样使用是不对的。