Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input value doesn't update if new value comes in nextProps #214

Open
dzmitry-kremez-itechart opened this issue Aug 16, 2016 · 1 comment

Comments

@dzmitry-kremez-itechart
Copy link

dzmitry-kremez-itechart commented Aug 16, 2016

For example I have cities typeahead input that is updated when user change another zip code input, so I need to set new input value by new property, but as it is based on state it doens't update it. I fixed it in my app by:

<Typeahead
    ...
    ref={(ref) => this.typeahead = ref}
 />
componentDidMount() {
  if (this.typeahead) {
    this.typeahead.componentWillReceiveProps = function (nextProps) {
      this.setState({
        entryValue: nextProps.value,
        visible: this.getOptionsForValue('', nextProps.options),
      });
    };
  }
}

I think this code could be placed inside the src. Is there any reason that why not to update entryValue state from new props?

@Glogo
Copy link

Glogo commented Sep 7, 2016

This is bugging me for quite some time. For those who prefer different solution (hotfix) this is my suggestion:

  • Wrap Typehead into your component and add value as your property
  • Implement componentWillReceiveProps and when your value changes, just setState with new entryValue
import { Typeahead } from 'react-typeahead';

class MyTypeahead extends React.Component {
    componentWillReceiveProps(nextProps) {
        if (this.props.value !== nextProps.value) {
            console.log('valueChanged', nextProps.value);
            this.refs.typeahead.setState({entryValue: nextProps.value});
        }
    }

    render() {
        return <Typeahead ref="typeahead" value={this.props.value} />
    }
}

MyTypeahead.propTypes = {
    value: PropTypes.string // Or whatever
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants