【发布时间】:2022-01-12 04:42:15
【问题描述】:
我正在尝试创建一个表格组件,该组件将为其特定的子级提供一些特殊的类名。
const BasicTable = ({ children }) => {
const RenderChild = Children.map(children, (el) => {
const child = el;
if (child !== null) {
if (child.props.originalType !== "th") {
return <child.type {...child.props} className="th" />;
}
return <child.type {...child.props} />;
}
return null;
});
return (
<div className="table-responsive">
<table className="table w-full bg-transparent">{RenderChild}</table>
</div>
);
};
这是我的组件,我想这样使用它。
<BasicTable>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>dsfa</td>
<td>dsfa</td>
<td>dsfa</td>
<td>dsfa</td>
</tr>
</tbody>
</BasicTable>
但这里的问题是我的 Children.map 函数仅循环通过其直接子级。如何将 props 传递给它的嵌套子项(th、td、...等)
【问题讨论】:
-
认为您必须检查子组件是否有其子组件并执行您想要的操作。例如,如果您正在迭代 thead,请检查 thead 是否有孩子,如果有,您可以执行您需要的操作
标签: javascript reactjs