Search code examples
reactjsreact-hooksevent-handling

Additional arguments for React event handlers (using functional components)


I want to pass an additional argument to onChange (event handler) of an <input> tag, apart from the event object. How do I do that?

Suppose, I have such a function

const f = (event, i) => { //stuff }

Can I do this? (I know it doesn't work)

<input onChange=f(1) />

Assuming, the event object gets passed in as the first argument automatically?

Note - I am using functional components


Solution

  • To pass an argument to onChange handler of tag, you can do the following:

    <input type="text" onChange={ (event)=>f(event,i) } />
    

    Although event object is automatically available, you will have to pass it to the function so as to make use of it.