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?

3
  • Possible duplicate of Call multiple functions onClick ReactJS Commented Jun 14, 2019 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? Commented Jun 14, 2019 at 18:58
  • i don't get any error.this functions do not execute i call multiple functions. Commented Jun 14, 2019 at 19:02

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.