【问题标题】:How do I auto-save with multiple form fields in React?如何在 React 中自动保存多个表单字段?
【发布时间】:2016-10-27 14:49:20
【问题描述】:

我在输入文本字段上有一个 onBlur 自动保存记录;数据结构是一个包含一系列项目的文档。如果我快速更改项目的第一列,然后快速更改第二列,则第一列的保存将导致更新,这将重新加载两个输入字段。

如何防止第一次保存重新加载整行以便保存两个列值?

这是代码 sn-p(我用setTimeout 模仿了服务器保存):

class Store {
  static doc = {title: "test title", items: [{id: 1, one: "aa", two: "bbb"}, {id: 2, one: "yyy", two: "zzz"}]};

  static getDocument() {
    return this.doc;
  }

  static saveDocument(doc) {
      this.doc = doc;
  }

  static updateItemInDocument(item, callback) {
      var foundIndex;
      let updatedEntry = this.doc.items.find( (s, index) => {
        foundIndex = index;
        return s.id === item.id;
      });

      this.doc.items[foundIndex] = item;
      setTimeout(() => { console.log("updated"); callback(); }, 1000);
  }
};

const Row = React.createClass({
  getInitialState() {
    return {item: this.props.item};  
  },

  update() {
    let document = Store.getDocument();

    let updatedEntry = document.items.find( (s) => {
            return s.id === this.props.item.id;
        } );

     this.setState({ item: updatedEntry});
  },

  handleEdit() {
    this.setState({item: {
        id: this.props.item.id,
        one: this.refs.one.value,
        two: this.refs.two.value
      }
    });  
  },

  handleSave() {
    Store.updateItemInDocument(this.state.item, this.update);
  },

  render() {
    let item = this.state.item;
    console.log(item);
    return  <tr> <p>Hello</p>
        <input ref="one" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.one} />
        <input ref="two" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.two} />
      </tr>;
  }
});

const App = React.createClass({
  render() {
    let rows = Store.getDocument().items.map( (item, i) => {
      return <Row key={i} item={item} />;
    });

    return <table>
      {rows}
      </table>;
  }
});

ReactDOM.render(
  <App />,
  document.getElementById("app")
);

我也有代码作为codepen:http://codepen.io/tommychheng/pen/zBNxeW?editors=1010

【问题讨论】:

  • item 总是并且只有属性 onetwo?或者item 可以有更多或更少的属性吗?
  • item 有固定数量的属性(只有一个和两个)和输入字段。

标签: javascript reactjs flux autosave


【解决方案1】:

问题源于tabbing out of an input field fires both the onChange and onBlur handlers

也许我误解了您的用例,但这是我解决此问题的方法:

const Row = React.createClass({
  getInitialState() {
    return { item: this.props.item };
  },

  handleSave() {
    let item = {
      id: this.state.item.id,
      one: this.refs.one.value,
      two: this.refs.two.value
    }
    Store.updateItemInDocument(item); // Is it really necessary to use the callback here?
  },

  render() {
    let item = this.state.item;
    return (
      <tr>
        <p>Hello</p>
        <input ref="one" type="text" onBlur={this.handleSave} defaultValue={item.one} />
        <input ref="two" type="text" onBlur={this.handleSave} defaultValue={item.two} />
      </tr>
    );
  }
});

我从使用onChange 处理程序切换到using a defaultValue

【讨论】:

  • 谢谢阿席达卡!我不知道默认值。它确实解决了更新后更新覆盖值的问题。我的问题看起来类似于stackoverflow.com/questions/37946229/…,其中评论者建议使用defaultValue 会导致uncontrolled component,这是一种不好的做法。你对uncontrolled component 有什么想法吗?
  • 我不认为使用defaultValue 是一个坏习惯。这取决于您的用例。在这种情况下,我认为使用defaultValue 是完全有效的。
猜你喜欢
  • 2018-12-21
  • 1970-01-01
  • 2016-12-03
  • 2017-04-04
  • 2012-07-23
  • 1970-01-01
  • 2018-09-30
  • 2011-07-17
  • 1970-01-01
相关资源
最近更新 更多