reactjs - 键入 React 组件实例的数组
问题描述
我有一个表格组件,它接收一个字符串/数字数组的数组作为道具,所以它需要一个rows
像这样键入的参数:
type Row = (string | number)[];
但是,有些列需要显示 React 组件而不是纯文本/数字。
如何更改类型以使其正常工作?我试过这个:
type Row = (string | number | React.PureComponent | React.Component)[];
解决方案
这里有许多可行的选项,其中一些比其他选项更好,具体取决于您如何使用它们。我一直在使用React.ReactNode
它来指定孩子,但您也可以将它用于您的用例。
type Row = (string | number | React.ReactNode)[];
ReactNode
非常灵活,因为它被定义为:
type ReactNode = ReactChild | ReactFragment | ReactPortal | boolean | null | undefined;
ReactChild
反过来,定义为:type ReactChild = ReactElement | ReactText;
如果您希望它更严格、更不灵活,请尝试ReactElement
.
推荐阅读
- javascript - 如何在laravel中使用javascript在没有预览对话框的情况下打印
- python - 遍历一个列表并找到所有属性的 ID:
- flutter - 让蓝牙设备自动连接和断开的方法是什么?
- javascript - 如果我要查找的值在数组中的对象中,我想通过 javascript 返回 true
- python - 使用 Python 进行图像比较或分类
- javascript - 如何在 vuejs 中添加标题?
- php - 我创建了新的扩展并放置在市场上,但它给出了错误 MFTF AdminCreateAndEditConfigurableProductSettingsTest is broken
- c# - C# - 嵌套的 LINQ 查询
- python - 如何从训练数据中预测/推断正态分布
- java - 如何使用千分尺registry.counter