gpt4 book ai didi

reactjs - 你可以在 React.Fragment 上使用 props 吗?

转载 作者:行者123 更新时间:2023-12-03 19:43:33 24 4
gpt4 key购买 nike

我知道 React Fragment 可用于对子组件进行分组,如下所示:

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

但是你可以给它添加 Prop 吗?假设您要使用扩展运算符:

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

最佳答案

不,你不能。根据 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。

关于reactjs - 你可以在 React.Fragment 上使用 props 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59225628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com