简体   繁体   English

ReactJS onClick 不触发

[英]ReactJS onClick not firing

I have:我有:

... ...

setActive(section) {
        this.setState({
            currentSection: section
        });
        console.log('Current view is'+this.state.currentSection);
    }

    render() {
        return <div>
            <div className="section">
                <HeaderButton active text="test1" count={23} backgoundColor={'#c04c36'} onClick={() => this.setActive.bind('test1')}/>
            </div>
            <div className="section">
                <HeaderButton text="test2" count={45} backgoundColor={'#ffe698'} onClick={() => this.setActive.bind('test2')}/>
            </div>
            <div className="section">
                <HeaderButton text="test3" count={4} backgoundColor={'#198a75'} onClick={() => this.setActive.bind('test3')}/>
            </div>
        </div>
    }

But when I click on those component nothing at all happens.但是当我点击这些组件时,什么也没有发生。 What am I doing wrong?我究竟做错了什么?

The problem is that you are both using the arrow function, and binding as well.问题是您同时使用箭头函数和绑定。 You are also not binding to an execution context.您也没有绑定到执行上下文。

This is a confusing concept.是一个令人困惑的概念。

When you call an onClick without an arrow function, you need to bind it.当你调用一个没有箭头函数的 onClick 时,你需要绑定它。

Thus, a call like this...因此,这样的电话...

onClick = {this.setActive.bind(this)}

Needs to be called or else this.setActive will lose its binding.需要调用,否则 this.setActive 将失去其绑定。 It is bound to the execution context that you would like it to run in, in this case that being this它绑定到您希望它运行的执行上下文,在这种情况下就是这个

An ES6 arrow function is lexically bound and does not need to be bound to an execution context. ES6 箭头函数是词法绑定的,不需要绑定到执行上下文。

thus, you can have...因此,您可以拥有...

onclick ={() => this.setActive()}

Which will automatically run in the context where it is written, thus not needing the binding.它将在编写它的上下文中自动运行,因此不需要绑定。

Also, you are binding to a string instead of an execution context (usually a component).此外,您绑定到一个字符串而不是一个执行上下文(通常是一个组件)。

Take out the bind and your function should run.取出绑定,您的函数应该运行。

您应该在构造函数中绑定事件,而不是在属性本身中。

Avoid calling .bind() in the onclick handler itself.避免在onclick处理程序本身中调用.bind() Instead, you can call setActive() directly via the this variable seeing that the context of the arrow function is that of your component:相反,您可以通过this变量直接调用setActive()看到箭头函数的上下文是您的组件的上下文:

render() {
    return <div>
        <div className="section">
            <HeaderButton active text="test1" count={23} 
                          backgoundColor={'#c04c36'} 
                          onClick={() => this.setActive('test1')}/>
        </div>
        <div className="section">
            <HeaderButton text="test2" count={45} 
                          backgoundColor={'#ffe698'} 
                          onClick={() => this.setActive('test2')}/>
        </div>
        <div className="section">
            <HeaderButton text="test3" count={4} 
                          backgoundColor={'#198a75'} 
                          onClick={() => this.setActive('test3')}/>
        </div>
    </div>
}

Alternatively, to optimize your render() method, you could create pre-bound function instances to avoid use of arrow functions in your render method:或者,为了优化render()方法,您可以创建预绑定函数实例以避免在渲染方法中使用箭头函数:

constructor(props) {
 super(props);

 /* Bind setActive to this component/class instance */
 this.setActiveTest1 = this.setActive.bind(this, 'test1')
 this.setActiveTest2 = this.setActive.bind(this, 'test2')
 this.setActiveTest3 = this.setActive.bind(this, 'test3')
}

setActive(section) {
    this.setState({
        currentSection: section
    });
    console.log('Current view is'+this.state.currentSection);
}

render() {
    return <div>
        <div className="section">
            <HeaderButton active text="test1" count={23} 
                          backgoundColor={'#c04c36'} 
                          onClick={this.setActiveTest1 }/>
        </div>
        <div className="section">
            <HeaderButton text="test2" count={45} 
                          backgoundColor={'#ffe698'} 
                          onClick={this.setActiveTest2}/>
        </div>
        <div className="section">
            <HeaderButton text="test3" count={4} 
                          backgoundColor={'#198a75'} 
                          onClick={this.setActiveTest3}/>
        </div>
    </div>
}

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

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