React 限制了渲染的数量

react limits the number of renders

在 React 中,我正在展示产品 I.E.

{renderPosts.map((项目, 索引) => ( ...

现在,在每个产品中,都有一个删除该产品的按钮,在我点击按钮内调用 sweetAlert 来删除特定产品:

<Button
  color="danger"
  simple
  justIcon
  onClick={warningWithConfirmMessage}
 >

那样一切正常...但是如果我想传递必须删除的产品的 ID...

<Button
  color="danger"
  simple
  justIcon
  onClick={warningWithConfirmMessage(item.postId)}
 >

现在我遇到了错误:

重新渲染太多。 React 限制渲染次数以防止无限循环。

我如何传递任何功能...任何人

提前致谢!

您传递的新 onClick 处理程序每​​次呈现时都会调用 warningWithConfirmMessage。要将函数作为处理函数传递而不是调用它,请使用:

onClick={() => warningWithConfirmMessage(item.postId)}