reactjs 为 elasticsearch 置换了事件

reactjs displaced events for elasticsearch

我将 ElasticSearch 路由连接到我的 ReactJS 服务 CodeService.js。这样 API 就会在每次输入字段中的键时被调用。效果很好。当我记录每个阶段时,我可以在网络和控制台中看到新对象得到了很好的处理。

而不是 Order.jsx。在那里,我总是落后一击。 当我打例如0 比我清楚地看到调用了 API 并且它转到了 CodeStore.js 但是在 Order.jsx 中调用 this.getCodeState().options 提示:[]直到我按下第二个键,我得到的是我以前的商店版本,而不是实际版本。

我的假设是我的视图在商店更新之前呈现。我该如何针对这种行为?

这是我的代码:

  export default class Order extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
      inputValue: '',
      options: []
    };
    this.props = {
      inputValue: '',
      options: []
    };
    this._onChange = this._onChange.bind(this);
  }

  handleHint(){
    return 'blahblahblha';
  }

  handleComplete(){
    alert('complete!');
  }

  _onChange(event) {
    var enteredChars = event.target.value;
    this.setState({inputValue: enteredChars});
    CodeService.nextCode(enteredChars);
  }

  getCodeState() {
    return {
      options: CodeStore.postcode
    };
  }

  render() {
    return (
      <div className="newer">[...])}

我能够通过添加 componentDidMount 并添加 eventchangelistener 来修复它。

componentDidMount() {
  CodeStore.addChangeListener(this._onChange);
}

唯一需要考虑的是你需要在你的_onChange事件中有一个条件,否则你有一个无限循环