有没有办法检查道具功能并将其传递到下一个级别

  • 本文关键字:下一个 功能 有没有 reactjs
  • 更新时间 :
  • 英文 :


嗨,我正在尝试将函数传递给容器树。假设我在 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>
</>
);
}

最新更新