prevProps.search.query始终与this.props.search.query相同

时间:2017-05-18 04:12:28

标签: javascript reactjs redux

在调度更新redux状态的操作后,

prevProps.search.query始终与this.props.search.query相同。

如果我在输入中输入'testing'并在this.props.search.query中记录prevProps.search.querycomponentDidUpdate(),则会打印'测试'

不应该prevProps.search.querytestin(没有h,因为它是上一个搜索查询)?

// results component

class Results extends React.Component {

  constructor (props) {

    super(props);

    this.state = {
      resultsClass: 'row search-results',
      results: null,
      searchQuery: ''
    }
  }

  componentDidUpdate(prevProps, prevState){

    if(this.props.search.query !== prevProps.search.query){
      this.getSearchResults();
    }

  }

}

// input component

<input type="text" onChange={this.handleSearchUpdate} />

handleSearchUpdate() {

  this.props.dispatch(setSearchQuery(this.queryInput.value));

}

在我的减速机中,我正在设置状态

case constants.SET_SEARCH_QUERY:
        return merge({}, set(state, 'search.query', action.query));

1 个答案:

答案 0 :(得分:0)

我认为你错误地将它与componentWillReceiveProps混淆了。 如果您需要componentWillReceiveProps(nextProps)

,则应使用nextProps

在此处了解详情https://facebook.github.io/react/docs/react-component.html#componentwillmount

相关问题