0

I have a button as below

<a href="#" className={classes.btnText} onClick={showDetail}>Learn more <span> &rarr; </span> </a>

and able to get the event object

const showDetail = event => {
        console.log("show detail",event);
    }

Now i want to pass an argument on button click. can some one please tell me how to pass event plus an parameter on button click like showDetail(event, "test") and i dont want the function to be invoked until the button is clicked

2 Answers 2

4
<a href="#" className={classes.btnText} onClick={(e) => showDetail(e, "test")}>Learn more <span> &rarr; </span> </a>

const showDetail = (event, additionalData) => {
        console.log("show detail", additionalData, event);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Make it a function which returns a function

const showDetail = type => event => {
  console.log("show detail", type, event);
}
<a href="#" className={classes.btnText} onClick={showDetail("test")}>
  Learn more <span> &rarr; </span>
</a>

this is a currying function, the type variable can be accessed from event callback

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.