javascript - 将函数传递给另一个组件时,onClick 道具不起作用
问题描述
我有一个这样的组件
<CardAvReport
buttonProps={buttonProps ? { ...buttonProps , small: true, onClick: () => console.log("hi")} : null}
onHandlerExample={this.someotherFunction}
includeLink
showChildren
{...rest}
>
这个组件渲染了一个div,这个div包含了一个由onClick触发的按钮。
如果单击,我可以看到 console.log("hi") 被触发。问题是如果我调用这样的函数
<CardAvReport
buttonProps={buttonProps ? { ...buttonProps , small: true, onClick: () => this.myFunction} : null}
onHandlerExample={this.someotherFunction}
includeLink
showChildren
{...rest}
>
它不会工作。
在 CardAvReport 中,buttonprops 最终出现在另一个组件中
<ButtonLink {...buttonProps}>{buttonProps.text}</ButtonLink>
该组件定义为
import * as React from "react";
import Button from "./Button";
export default props => {
const { link, onClick, children, ...rest } = props;
let props2 = { onClick };
if (link) props2["href"] = link;
if (onClick) props2.onClick = onClick;
return (
<a {...props2}>
<Button {...rest}>{children}</Button>
</a>
);
};
我不明白如何调试它,以及为什么一个函数在 onClick 中不起作用。
解决方案
推荐阅读
- virtualbox - 如何在 VirtualBox 中创建虚拟 IP 或端口?
- c# - 如何使父节点无法在树视图中选择?
- networkx - 恢复具有最大入度的节点并检索其经度和纬度
- javascript - 为什么组件在 this.setState() 设置完成之前渲染?
- python-3.x - 根据其他列条件将当前值连接到先前值
- swift - 为 coreML 模型提供图像时出现运行时异常
- excel - vba 形状重绘
- python - 制作一个python程序来拍摄一张照片并通过gmail发送
- javascript - 替换所有使用 @[word] 以链接到 String 中的 Twitter 帐户
- javascript - 字体仅在先前在页面中使用后出现在画布上