我们可以将点击处理程序附加到自定义子组件吗 [英] can we attach click handlers to custom child components

查看:27
本文介绍了我们可以将点击处理程序附加到自定义子组件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向我自己的子组件添加一个点击处理程序.在 react chrome 扩展中,我也能看到点击处理程序.

但是点击本身不起作用 - 想知道我错过了什么.

示例代码:

<预><代码>...使成为 (<MySampleComponent onClick={this.handler}/>);...

解决方案

MySampleComponent 可以接受它想要的任何道具;组件不会自动将 props 复制到它们的孩子.如果您希望能够向 MySampleComponent 添加一个 onClick 处理程序,那么您可以在该组件的定义中支持这一点:

var MySampleComponent = React.createClass({渲染:函数(){return <div onClick={this.props.onClick}>...</div>;}});

I was trying to add a click handler to my own child component. In react chrome extension I was able to see the click handler as well.

But the click itself didn't work - wondering what did I miss.

Sample Code:

... 
render (
  <MySampleComponent onClick={this.handler} />
);
...

解决方案

MySampleComponent can take whichever props it wants; components don't automatically copy props to their children. If you want to be able to add an onClick handler to MySampleComponent, then you can support this in the definition of that component:

var MySampleComponent = React.createClass({
  render: function() {
    return <div onClick={this.props.onClick}>...</div>;
  }
});

这篇关于我们可以将点击处理程序附加到自定义子组件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆