0

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.

   //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.

<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?

ariful Islam
  • 107
  • 3
  • 12
  • Possible duplicate of [Call multiple functions onClick ReactJS](https://stackoverflow.com/questions/26069238/call-multiple-functions-onclick-reactjs) – thmsdnnr Jun 14 '19 at 18:56
  • It would be nice if you expanded on "it doesn't work" do you get an error? are your functions called at all? – Andrew Lohr Jun 14 '19 at 18:58
  • i don't get any error.this functions do not execute i call multiple functions. – ariful Islam Jun 14 '19 at 19:02

3 Answers3

1

saveValue function needs event argument. So try this:

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

Also, instead onclick should be onClick

m51
  • 1,950
  • 16
  • 25
1

It's because in your first example you typed onclick instead of onClick. Notice the capitalization of C. React event handlers are camel-cased.

The Qodesmith
  • 3,205
  • 4
  • 32
  • 45
  • 1
    I did that part right on my code.sorry for that unwanted error here.The problem was with event argument. First reply answers my questions.Thank you – ariful Islam Jun 14 '19 at 19:11
1

You need to use onClick instead of onclick. This is a difference in HTML and Reactjs.

For more details follow this link: https://reactjs.org/docs/handling-events.html

Also check this out: Call multiple functions onClick ReactJS

<button onClick={()=>{this.saveValue();this.goToLastPage()}}>Residence</button>
Aditya Parmar
  • 1,139
  • 13
  • 22