Passing an additional parameter with an onChange event

kojow7 :

What I'm trying to do:

I am trying to pass a string from a child component to the handleChange function of a parent component.

What currently works:

I have a parent React JS class with the following method:

handleChange(event) {

    console.log(event.target.value);
}

In the render function I have the following:

<Child handleChange={this.handleChange.bind(this)} />

In the Child class I have:

<fieldset onChange={this.props.handleChange}>
    <div>Tag 1: <input id="tag1" value={tags[0]} /></div>
    <div>Tag 2: <input id="tag2" value={tags[1]} /></div>
    <div>Tag 3: <input id="tag3" value={tags[2]} /></div>
</fieldset>

This works fine.

What I am trying to do instead:

I am attempting to add a section parameter to the handleChange function as follows:

handleChange(section, event) {
    console.log(section);
    console.log(event.target.value);
}

And in the Child class I have:

<fieldset onChange={this.props.handleChange("tags")}>
    <div>Tag 1: <input id="tag1" value={tags[0]} /></div>
    <div>Tag 2: <input id="tag2" value={tags[1]} /></div>
    <div>Tag 3: <input id="tag3" value={tags[2]} /></div>
</fieldset>

I now get the error:

Uncaught TypeError: Cannot read property 'target' of undefined

This error is being thrown in my second console.log statement.

What am I doing wrong?

Also, I am considering making the section parameter optional. If so, is there an easy way to do this? It seems like it might not be possible if the event parameter needs to be last.

Ritesh Bansal :

When you are writing this:

<fieldset onChange={this.props.handleChange("tags")}>

handleChange will be called immediately as soon as render is triggered.

Instead, do it like this:

<fieldset onChange={(e) => this.props.handleChange("tags", e)}>

Now the handleChange will be called when onChange handler is called.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing parameter to onchange event in Rails

Passing additional parameter through Event Listener

Passing both event and parameter to onchange bound function in JavaScript

Passing an additional argument to an event handler

passing this object as argument in onChange event?

passing an additional parameter to a function when function is called via event i.e :Connect()

passing parameter for event listener

Passing a parameter into an event handler

Passing Event handler as parameter

Passing additional arguments to an event callback function

Passing an event and additional variable in React Native

Uploading a file and passing a additional parameter with multer

React passing onChange event down to child component

Passing C# attributes to onChange event

Multiple onchange effects in a for loop passing parameter

Passing a function as a parameter that will be an onClick event

Passing custom EventArgs as parameter of event

Passing a function with parameter to event handler

Send additional parameter to Ajax event listener

Adding event handler for button in the ListView item template, passing additional arguments

passing data from laravel view to controller via ajax onchange event

Passing Parameters to a function on an onChange Event along with field data

React Selector: passing onChange event back through two components

onClick event fires infinite when passing a parameter

Gtag Event Parameter not passing complete data

How to passing the parameter to event handle in reactjs

Javascript - onClick event passing same parameter to function

Passing a delegate/event as a ref generic parameter

Passing event parameter when clicking a div in React