最好先看看我的代码:
import React, { Component } from 'react';
import _ from 'lodash';
import Services from 'Services'; // Webservice calls
export default class componentName extends Component {
  constructor(props) {
    super(props);
    this.state = {
      value: this.props.value || null
    }
  }
  onChange(value) {
    this.setState({ value });
    // This doesn't call Services.setValue at all
    _.debounce(() => Services.setValue(value), 1000);
  }
  render() {
    return (
      <div>
        <input 
          onChange={(event, value) => this.onChange(value)}
          value={this.state.value}
        />
      </div>
    )
  }
}
只是一个简单的输入。在构造函数中,它value从props(如果可用)中抓取并为组件设置本地状态。
然后在I的onChange函数中input更新状态,然后尝试调用 webservice 端点以设置新值Services.setValue()。
什么工作是如果我debounce直接通过onChange输入设置 像这样:
<input 
  value={this.state.value} 
  onChange={_.debounce((event, value) => this.onChange(value), 1000)} 
/>
但随后this.setState仅每 1000 毫秒调用一次并更新视图。因此,在文本字段中输入最终看起来很奇怪,因为您输入的内容仅在一秒钟后显示。
遇到这种情况我该怎么办?