简体   繁体   中英

prevProps.search.query is always the same as this.props.search.query

prevProps.search.query is always the same as this.props.search.query after dispatching action to update redux state.

If I type 'testing' in the input and log this.props.search.query and prevProps.search.query in componentDidUpdate() they will both print 'testing'

Shouldn't prevProps.search.query be testin (without the h as it was the prev search query)?

// 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));

}

In my reducers, I am setting the state

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

I think u mistaken it with componentWillReceiveProps. You should use componentWillReceiveProps(nextProps) , if you need nextProps

learn more here https://facebook.github.io/react/docs/react-component.html#componentwillmount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM