嗨,我正在尝试将函数传递给容器树。假设我在 A 中定义一个函数,将其传递给 B,然后从 B 将其传递给 C。现在我想在 B 中检查我是否传递该函数,如果是,我将其向下传递。有什么办法吗?
下面是 B 函数:
function CategoriesTable({ data, deleteContent }) {
return (
<>
<Table responsive>
<thead>
<tr>
<th>Category Name</th>
<th className='text-center'>Count</th>
<th className='actions'></th>
</tr>
</thead>
<tbody>
{data.map((category) => (
<CategoriesTableRow
category={category}
deleteContent={(id) => deleteContent(id)}
/>
))}
</tbody>
</Table>
</>
);
}
现在我想验证我是否获得了删除功能?我该怎么做?
您可以在调用函数之前检查该函数
deleteContent={(id) => deleteContent && deleteContent(id)}
或设置默认参数
function CategoriesTable({ data, deleteContent = () => null })
嗨,你可以这样检查
function CategoriesTable({ data, deleteContent = () => null }) {
console.log("Am i getting the function",deleteContent); //to check only by printing it on console
return (
<>
<Table responsive>
<thead>
<tr>
<th>Category Name</th>
<th className='text-center'>Count</th>
<th className='actions'></th>
</tr>
</thead>
<tbody>
{data.map((category) => (
<CategoriesTableRow
category={category}
deleteContent={(id) => deleteContent && deleteContent(id)}
/>
))}
</tbody>
</Table>
</>
);
}