错误:(数据:字符串)=>数字'不能分配给类型'()=> void'
尝试将字符串推到数组时遇到错误。 所有的帮助都得到赞赏。
错误消息 @ onclick:
type'(productName:string)=>数字“不能分配给type'()=> void'.ts(2322) type.d.ts(59,5):预期类型来自属性'onClick',该属性在此处声明为'IntinerInsiCattributes& amp; ButtonProps'
下方包含了关联的代码
const productList : string[] = [];
const handleClick = useCallback((productName:string) => productList.push(productName),[]);
我已经在JSX
<Button primary onClick={handleClick}>Add Product</Button>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该按钮如何知道您单击的产品?
答案是,您需要为每个按钮创建一个新功能才能传递到点击的产品中。
例如:
请参阅游乐场
最后注意
productList
应该是状态,而不是一个简单的变量。如果您只是将东西推入数组,React将永远不会注意到,并且永远不会重新渲染任何内容。这完全是一个不同的问题,但我想我应该提到。How does the button know what product you clicked?
The answer is that you need make a new function for each button to pass in the product that was clicked.
For example:
See playground
Final note that
productList
should be state and not a simple variable. If you just push things into array, react will never notice, and never re-render anything. That's a different problem entirely, but figured I should mention it.