How to pass Value to an Event Handler (React.js)

Context:



If you have a drop down menu and you want to call a function when a different option is selected (eg. slide 2), you can use the onChange attrbute.



If you then want to pass that option's value (eg. 2), you don't actually have to pass it as a parameter. The value is passed automatically in an object called event.

event is the object that has all the information about the event, ie. the user chose another option. event has a property called target, which returns the element that triggered the event.

In this case, target refers to the select tag, so event.target.value gives you the value that was chosen (eg.2)



bind

You may be wondering what bind does and why we need it. bind ties together this and the skipToSlide method, so that this (when inside the method) refers to the select tag rather than the entire object that the method belongs to.

Comments

Unknown said…
Hi, your blog really nice, keep it up! I’ll go ahead and bookmark your blog to come back later.
Regards,
ReactJS Consult
Laily Ajellu said…
Thanks for the feedback!

Popular Posts