简体   繁体   English

多个onClick无效反应

[英]multiple functions onClick not working reactJs

I am trying to call two multiple functions onClick.It doesn't work when i try to do it.But it works in single function.these are the two functions. 我正在尝试调用onClick的两个多功能函数。当我尝试这样做时不起作用。但是它只能在单个函数中使用,这是两个函数。

   //first function
saveValue = (e) => {
    // console.log('savecategory', e.target.innerHTML);

    this.setState({
        category: e.target.innerHTML
    }, this.makeAxiosRequest);

};

//second function

goToLastPage = () => {
    const {currentPage, pages, end} = this.state;
};

This is the onClick event i am trying to work. 这是我正在尝试的onClick事件。

<button   onclick={()=>{this.saveValue();this.goToLastPage()}}>Residence</button>

But it doesn't work.It only works if i call it as a single function like this. 但这是行不通的,仅当我将其作为单个函数调用时才起作用。

<button onClick={this.saveValue}>Residence</button>

<button onClick={this.goToLastPage}>Residence</button> 

How can i make it work? 我该如何运作?

saveValue function needs event argument. saveValue函数需要事件参数。 So try this: 所以试试这个:

<button onClick={(e) => { this.saveValue(e); this.goToLastPage(); }}>Residence</button>

Also, instead onclick should be onClick 另外, onclick应该是onClick

It's because in your first example you typed onclick instead of onClick . 这是因为在第一个示例中,您键入的是onclick而不是onClick Notice the capitalization of C . 注意C的大写。 React event handlers are camel-cased. React事件处理程序是驼峰式的。

You need to use onClick instead of onclick. 您需要使用onClick而不是onclick。 This is a difference in HTML and Reactjs. 这是HTML和Reactjs的区别。

For more details follow this link: https://reactjs.org/docs/handling-events.html 有关更多详细信息,请访问以下链接: https : //reactjs.org/docs/handling-events.html

Also check this out: Call multiple functions onClick ReactJS 还要检查一下: 调用onClick ReactJS的多个函数

<button onClick={()=>{this.saveValue();this.goToLastPage()}}>Residence</button>

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

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