Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an additional parameter with an onChange event

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.

like image 585
kojow7 Avatar asked Jul 05 '17 05:07

kojow7


People also ask

Can we pass two functions onChange event?

You can only assign a handler to onChange once. When you use multiple assignments like that, the second one will overwrite the first one.

How do you pass parameters to an event handler React?

Passing the event object of react as the second argument. If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.

How do you pass a variable in a function in React?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...


2 Answers

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.

like image 113
Ritesh Bansal Avatar answered Oct 08 '22 14:10

Ritesh Bansal


In your handle event use double arrow function, there's no need to bind when using arrow function:

handleChange = tags => (event) => {     console.log(tags);     console.log(event.target.value); } 

And in the Child:

<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> 
like image 42
François Alexandre COLOMBANI Avatar answered Oct 08 '22 12:10

François Alexandre COLOMBANI