Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: avoid controlled component boilerplate

Tags:

reactjs

So I'm just asking out of curiosity,

Anyone found a clever way to handle 2-way data binding in controlled components (input, select, ...) without having to write all of the following:

  getInitialState: function() {
    return {value: 'Hello!'};
  },
  handleChange: function(event) {
    this.setState({value: event.target.value});
  },
  render: function() {
    var value = this.state.value;
    return <input type="text" value={value} onChange={this.handleChange} />;
  } 
like image 851
plus- Avatar asked Nov 12 '14 15:11

plus-


Video Answer


2 Answers

You may want to read "Two-Way Binding Helpers" section of the documentation: http://facebook.github.io/react/docs/two-way-binding-helpers.html

There is this LinkedStateMixin:

var NoLink = React.createClass({
  getInitialState: function() {
    return {message: 'Hello!'};
  },
  handleChange: function(event) {
    this.setState({message: event.target.value});
  },
  render: function() {
    var message = this.state.message;
    return <input type="text" value={message} onChange={this.handleChange} />;
  }
});


var WithLink = React.createClass({
  mixins: [React.addons.LinkedStateMixin],
  getInitialState: function() {
    return {message: 'Hello!'};
  },
  render: function() {
    return <input type="text" valueLink={this.linkState('message')} />;
  }
});
like image 164
nilgun Avatar answered Sep 29 '22 18:09

nilgun


A trick that's worth knowing - since the onChange event bubbles up, you can wrap form inputs in a container and register the onChange on that instead - <form> is perfect for this.

Then you can write a generic onChange handler which pulls data from the event's target - you will need to add some identifying info to the field, which is what the name attribute is for anyway.

Here's a Gist with an example implementation and a live demo of it in action - form input state is displayed below the form.

like image 34
Jonny Buchanan Avatar answered Sep 29 '22 18:09

Jonny Buchanan