fmoo / react-typeahead

Pure react-based typeahead and typeahead-tokenizer
ISC License
677 stars 230 forks source link

defaultValue doesn't update after re-render #126

Open yasenov-casafari opened 9 years ago

yasenov-casafari commented 9 years ago

I have Typeahed object with defaultValue prop. In componentDidMount I receive new value and call setState but defaultValue doesn't change.

mCzolko commented 9 years ago

same problem here

trshafer commented 9 years ago

You may be falling for https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html . There's an unofficial API for setting the text: https://github.com/fmoo/react-typeahead/blob/master/src/typeahead/index.js#L108 I don't exactly know how it works in your case for componentDidMount, but for componentDidUpdate, something like this should work:

componentDidUpdate (prevProps, prevState){
  if (this.props.text != prevProps.text) { this.refs.MyTokenizer.setEntryText(this.props.text);}
}

I'd love a readme update PR if you get that working.