56

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.

1
  • 1
    I'm just wondering, isn't bind within a property an anti pattern in React? Jan 15, 2017 at 14:41

2 Answers 2

88

I think this way is easier:

    <Input type="text"
                value={range.name}
                onChange={(e) => this.changeRangeName(range.id, e)}
        ...
    onChange(id, e) {
        ...
    }
4
  • Thanks, I liked doing it this way as 1) my binding was in the constructor 2) It is more readable.
    – runios
    Sep 18, 2017 at 6:57
  • I agree this is very readable. But this implementation gives a warning in the browser saying: "A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component." Any thoughts on how to avoid this warning? Oct 25, 2017 at 21:27
  • 7
    The downside: a new instance of this anonymous function is create every time it renders, potentially a performance pitfall. What do you think?
    – Morris
    Jul 9, 2018 at 19:22
  • 1
    @Vic it looks from some blog posts that the arrow function and binding should be avoided in the render function. I am finding hard to determine when, if it works in some instances.
    – thehme
    Feb 6, 2019 at 20:54
46

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()

1
  • how to add multiple args like function (rageId, anotherValue, thirdValue, envent) Apr 26, 2020 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.