REACT event target value works but what about the ID?

I can’t seem to get the ID of the item I am trying to edit.

   eachPollResponse = (resp) => {
 
		const remove = () => {} 
		
		return (<PollDetail key={resp.respID} 
				id={resp.respID} onSubmit={this.submit} onChange={this.save}
				editMode={this.state.editing} 
				onRemove={remove} respText={resp.response}>
				{resp.response}</PollDetail>)
	}
	
	save = (event) => {
		event.preventDefault();
		this.setState({typed:event.target.value});
		alert("update " + event.target.value + " ID : "+ event.target.ref );
	}

I have tried various options event.target.ref , event.target.id but it’s undefined.
Passing a parameter to this save? Would that work. When I tried the whole thing broke.

What do you add the id to: DOM element or React element?

event.target references DOM element. You pass id to PollDetail Component. What happens inside of the component? Do you pass the id onto the input? Seems like you don’t.

If you just want to pass a parameter to the this.save function, you can do:
onChange={this.save.bind(respID)}

How am I supposed to do that?
I am trying:

Try event.target.getAttribute(“id”)

The ID is in several places now, none of which I can access from the save/onSubmit function or any other function for that matter.
I thought you were supposed to do this in the Parent component but would it be simpler to do an API update in the child component and then refresh / re-direct back to the page?
This is crazy.

Great, now how do I access it in the save function?

You’ve got at least 3 ways to make it work:

  • partial application
  • higher-order function / currying
  • passing the prop down into DOM element

Would you care to explain those, for the beginners out there (like me) ?

All the fun for me? Well, as you wish.

Didn’t run codes below so there may be bugs.

  • partial application
   eachPollResponse = (resp) => {
 
		const remove = () => {} 
		
		return (<PollDetail key={resp.respID} 
				id={resp.respID}
                onSubmit={this.submit}
                onChange={this.save.bind(null, resp.respID)} // applying first argument to save()
				editMode={this.state.editing} 
				onRemove={remove} respText={resp.response}>
				{resp.response}</PollDetail>)
	}
	
	save = (id, event) => { // save has two args now
		event.preventDefault();
		this.setState({typed:event.target.value});
		alert("update " + event.target.value + " ID : "+ id );
	}

  • higher-order function / currying
   eachPollResponse = (resp) => {
 
		const remove = () => {} 
		
		return (<PollDetail key={resp.respID} 
				id={resp.respID}
                onSubmit={this.submit}
                onChange={this.save(resp.respID)} // calling save with id
				editMode={this.state.editing} 
				onRemove={remove} respText={resp.response}>
				{resp.response}</PollDetail>)
	}
	
	save = (id) => (event) => { // save is a function that returns another function
		event.preventDefault();
		this.setState({typed:event.target.value});
		alert("update " + event.target.value + " ID : "+ id );
	}

  • passing the prop down into DOM element
renderForm() { // of PollDetail Component
	
			return (<div className="responseBox">
						<form onSubmit={this.props.onSubmit}>
						<input 
                          ref={this.props.key}
                         type="text" key={this.props.key}
						 className="form-control"
                         onChange={this.props.onChange}
////////////////////////////////////////////////
                         id={this.props.id} // passing id down to DOM element
///////////////////////////////////////////////
                         value={this.props.respText} />
						<input className="btn btn-success" type="submit" value="Update" />																
						<button className="btn btn-default" 
							onClick={this.cancel} >Cancel</button>
						</form>
					</div>)
	}



   eachPollResponse = (resp) => {
 
		const remove = () => {} 
		
		return (<PollDetail key={resp.respID} 
				id={resp.respID}
                onSubmit={this.submit}
                onChange={this.save} 
				editMode={this.state.editing} 
				onRemove={remove} respText={resp.response}>
				{resp.response}</PollDetail>)
	}
	
	save = (event) => {
		event.preventDefault();
		this.setState({typed:event.target.value});
		alert("update " + event.target.value + " ID : "+ event.target.id ); // now event.target.id should return id
	}

1 Like

Thanks. I will try the currying as I was attempting something like this before. (I got the syntax wrong I think.)
On first glance this seems to be working :thumbsup:

1 Like

thank you so much, you’ve save my life, stuck on this 3 hours, finally find your solution and solve my problem.
my problem was send id from component to onChange function, in the array list of data.

1 Like

In the end I have passed down functions as props which get assigned to each poll response with the response id. These are then used as props to the PollDetail component which handles editing of a single poll response.
See the finished file here: https://github.com/JohnnyBizzel/voting-app-final/blob/no-auth0/src/components/presentation/EditPoll.js

this.props.changetext(event.target.value, this.props.id) is used to pass the new values to the state (line 45)