React setState on textarea ignored

- 1 answer

Ad

I am working on a simple meteor + react CRUD app. In the code below, my this.setState() doesn't seem to have any effect on <textarea>. So when I click on a link and the form re-renders, the function clickLoadForm(appId) properly updates the state of <input> elements, but not <textarea>, even though the console.log clearly shows that there is content for all the fields. What did I do wrong?

App = React.createClass({
  mixins: [ReactMeteorData],

  getMeteorData() {
    return {
      applications: Applications.find({}, {sort: {createdAt: -1}}).fetch(),
      currentApplication: Applications.findOne({_id:this.props.router.params.appid}, {sort: {createdAt: -1}}),
    }
  },
  getInitialState: function() {
    return this.loadForm(this.props.router.params.appid);
  },
  loadForm(appId) {
    var currentApp = Applications.findOne({_id:appId});
    if(!currentApp) currentApp = {};
    return currentApp;
  },
  clickLoadForm(appId)
  {
    var currentApp = this.loadForm(appId);
    var state = new Object();
    var refs = this.refs;
    Object.keys(refs).map(function(prop,index){
      state[prop] = typeof currentApp[prop] == 'undefined' ? "" : currentApp[prop];
    });
    console.log(state);
    this.setState(state);
  },
  renderListApplications() {
    var _this = this;
    return this.data.applications.map(function(applicationform,i) {
      return <li key={"li"+i}><a onClick={_this.clickLoadForm.bind(_this,applicationform._id)} href={Meteor.absoluteUrl()+'application/' +applicationform._id} key={"a"+i}>Version {applicationform._id}</a></li>;
    });
  },
  handleSubmit(event) {
    event.preventDefault();
    var refs = this.refs;
    var formVals = new Object();
    Object.keys(refs).map(function(prop, index){
      if(refs[prop].nodeName.match(/(INPUT|SELECT|TEXTAREA)/).length > 0)
        formVals[prop] = refs[prop].value;
    });

    Meteor.call("saveApplication", formVals);

  },
  handleChange: function(e) {
    if(!e.target.id) return;
    if(typeof e.target.id == 'undefined') return;
    var state = new Object();
    state[e.target.id] = e.target.value;

    this.setState(state);
  },
  render() {
    return (
      <div className="container">
          <ul>
            {this.renderListApplications()}
          </ul>
          <div>{JSON.stringify(this.data.currentApplication)}</div>
          <form className="new-task" onSubmit={this.handleSubmit} >
            <input ref="input_36" id="input_36" type="text" value={this.state.input_36} onChange={this.handleChange} />
            <input ref="input_37" id="input_37" type="text" value={this.state.input_37} onChange={this.handleChange} />
            <textarea ref="input_38" id="input_38" onChange={this.handleChange}>{this.state.input_38}</textarea>
            <textarea ref="input_39" id="input_39" onChange={this.handleChange}>{this.state.input_39}</textarea>
            <button type="submit">Submit</button>
          </form>
      </div>
    );
  }
});
Ad

Answer

Ad

Actually I figured it out. I changed

<textarea ref="input_38" id="input_38" onChange={this.handleChange}>{this.state.input_38}</textarea>
<textarea ref="input_39" id="input_39" onChange={this.handleChange}>{this.state.input_39}</textarea>

to

<textarea ref="input_38" id="input_38" onChange={this.handleChange} value={this.state.input_38} />
<textarea ref="input_39" id="input_39" onChange={this.handleChange} value={this.state.input_39} />
Ad
source: stackoverflow.com
Ad