javascript - 如何在 React 中使用多个表单字段自动保存?

标签 javascript reactjs flux autosave

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

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

这是代码片段(我用 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")
);

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

最佳答案

问题源于 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 .

关于javascript - 如何在 React 中使用多个表单字段自动保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38022618/

相关文章:

javascript - 从 id 中删除整数

reactjs - 错误 发生意外错误 : "EACCES: permission denied

javascript - 如何使用 React 内联创建多个空元素

javascript - 访问从组件中的 Action 创建者返回的信息是否会破坏 Flux?

javascript - 客户端路由的缺点?

reactjs - Flux 操作可以访问商店吗?

javascript - knockout 示例模板示例不起作用

javascript - 如何在 Javascript 中转义 JSON?

javascript - Rails ejs 未定义方法

css - 如何判断 CSS 类是否被识别?