Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
141
rated 0 times [  142] [ 1]  / answers: 1 / hits: 78284  / 9 Years ago, tue, march 17, 2015, 12:00:00

I render collection of input elements for objects in array.



render: function() {
var ranges = [];
this.props.ranges.map(function(range, index) {
var rangeElement = <Input type=text
value={range.name} onChange={this.changeRangeName.bind(this)} />
ranges.push(rangeElement);
}, this);

// render ranges
}


this allows me to write onChange handler function:



changeRangeName: function (event) {
var newName = event.target.value;
},


but in this handler I need id of range object I want to change. So I could change change how I create input elements in render function and change:



var rangeElement = <Input type=text
value={range.name}
onChange={this.changeRangeName.bind(this, range.id)} />


Now my handler will receive range.id as parameter but now I don't have the newName value. I can get it using refs



var rangeElement = <Input type=text
ref={'range' + range.id}
value={range.name}
onChange={this.changeRangeName.bind(this, range.id)} />


This is the only solution I know but I suspect there is better one.


More From » reactjs

 Answers
27

The event argument is still passed, but the rangeId argument is prepended to the arguments list, so your changeRangeName method would look like



changeRangeName: function (rangeId, event) {
var newName = event.target.value;
},


See Function.prototype.bind()


[#67404] Sunday, March 15, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ross

Total Points: 477
Total Questions: 97
Total Answers: 98

Location: France
Member since Thu, May 6, 2021
3 Years ago
;