简体   繁体   English

ReactJS:onClick in子组件不会触发

[英]ReactJS : onClick in sub-component does not trigger

I am trying to create a sub-component in JSX which is a list of items with an onClick event and an event handler. 我正在尝试在JSX中创建一个子组件,它是一个带有onClick事件和事件处理程序的项目列表。 The event handler is never called. 永远不会调用事件处理程序。 Please help me figure out what i am doing wrong. 请帮我弄清楚我做错了什么。

  • The React Chrome developer tools extension shows an "onClick" event handler being created for each "li" React Chrome开发人员工具扩展程序显示为每个“li”创建的“onClick”事件处理程序
  • The context (this) is the "Constructor" object which has the "handleClick" function as a property. 上下文(this)是“构造函数”对象,它具有“handleClick”函数作为属性。

EDIT: Thanks to @FakeRainBrigand, i found out the problem was elsewhere. 编辑:感谢@FakeRainBrigand,我发现问题出在其他地方。 I figured out that when I hide/show the sub-component using an event handler in the parent component, it stops triggering the onClick event handler, since the parent's hide operation triggers. 我发现当我使用父组件中的事件处理程序隐藏/显示子组件时,它会停止触发onClick事件处理程序,因为父级的隐藏操作会触发。 JSBIN JSBIN

The JSX code. JSX代码。

var FilterList = React.createClass({
    handleClick: function(e){
        console.log(e);

    },
    render: function(){
        var ListItems = this.props.data.map(function(item){
            return (
                <li key={item.id} className="filter-item" onClick={this.handleClick}>
                    <span key={'img'+item.id} className={item.imgClass}></span>
                    <span key={'text'+item.id} className="item-text">{item.name}</span>
                </li>
            )

        }, this);
        return (
            <div className="filter-list">
                    <ul>
                        {ListItems}
                    </ul>
                </div>
            );
    }
});

The generated JS code: 生成的JS代码:

var FilterList = React.createClass({displayName: 'FilterList',
    handleClick: function(e){
        console.log(e);


    },
    render: function(){
        var ListItems = this.props.data.map(function(item){
            return (
                React.DOM.li( {key:item.id, className:"filter-item", onClick:this.handleClick}, 
                    React.DOM.span( {key:'img'+item.id, className:item.imgClass}),
                    React.DOM.span( {key:'text'+item.id, className:"item-text"}, item.name)
                )
            )

        }, this);
        return (
            React.DOM.div( {className:"filter-list"}, 
                    React.DOM.ul(null, 
                        ListItems
                    )
                )
            );
    }
});

You want to make your server/ajax call in componentDidMount, not componentWillMount. 您希望在componentDidMount中调用server / ajax,而不是componentWillMount。 You can check out why in the documentation here React Component Lifecycle 您可以在此处的文档中查看React Component Lifecycle的原因

What happens if you add a self = this; 如果你添加一个self = this会发生什么? and change the this.handleClick to self.handleClick? 并将this.handleClick更改为self.handleClick?

render: function() { render:function(){

    self = this;
    var ListItems = this.props.data.map(function(item){

        return (
            <li key={item.id} className="filter-item" onClick={self.handleClick}>
                <span key={'img'+item.id} className={item.imgClass}></span>
                <span key={'text'+item.id} className="item-text">{item.name}</span>
            </li>
        )

    }, this);

maybe use callback function. 也许使用回调函数。 use callback on onclick like this: onclick上使用回调如下:

var FilterList = React.createClass({
handleClick: function(e){
    console.log(e);

},
render: function(){
    var ListItems = this.props.data.map(function(item){
        return (
            <li key={item.id} className="filter-item" onClick={() => this.handleClick}>
                <span key={'img'+item.id} className={item.imgClass}></span>
                <span key={'text'+item.id} className="item-text">{item.name}</span>
            </li>
        )

    }, this);
    return (
        <div className="filter-list">
                <ul>
                    {ListItems}
                </ul>
            </div>
        );
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM