How to pass a value to onClick event handler in React.js - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Tuesday, August 14, 2018

How to pass a value to onClick event handler in React.js

If you ever tried to pass a parameter to onClick event handler, you know that it is not straightforward.If you didn't, here is why may want to. Imagine a scenario where you have a group of three buttons. When you click on one of them, you want to know which one was clicked and perform an appropriate action. You have a choice of creating three onClick event handlers, one for each button, or one hander for all the buttons and pass a parameter identifying the clicked button into it. Which option do you choose? Clearly writing one handler should be less work, right?Unfortunately, passing a parameter into an event handler in React is not straightforward. If it were, there wouldn't be multi-page discussions about just that. By the way, the accepted answer on that Stackoverflow thread lists an "Easy way" along with "Better way" and an "Old easy way". The "Easy way" is indeed easier, but has a performance impact. And a "Better way" is in fact not that easy. Is that confusing, or is that just me?However, don't despair! I will show you how you can pass not even one, but multiple parameters into an onClick event handler.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to

javascript,front-end-development,react

No comments:

Post a Comment