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

When DayPickerInput get new props, it doesn't change its state #363

Closed
youreme opened this issue May 25, 2017 · 8 comments
Closed

When DayPickerInput get new props, it doesn't change its state #363

youreme opened this issue May 25, 2017 · 8 comments

Comments

@youreme
Copy link

youreme commented May 25, 2017

Hi,

When DayPickerInput gets nextProps, it doesn't change its states.
How about adding componentWillReceiveProps lifecycles?

After it mounted,
if it doesn't trigger any change events, the value state is different from value property.

DayPickerInput

componentWillReceiveProps(nextProps) {
  this.state = getStateFromProps(nextProps);
}
@infomiho
Copy link

+1

@gpbl
Copy link
Owner

gpbl commented May 25, 2017

Thinking about the use case here. Just resetting the state when the component receives new props doesn't work because the method it is always called when re-rendering the component. So we should add a condition there, like:

componentWillReceiveProps(nextProps) {
  if (nextProps.value !== this.state.value) {
    this.setState(getStateFromProps(nextProps));
  }
}

however this will cause the overlay to flicker (hide/show) when passing a value from the parent component, e.g. in this example. A similar issue would appear if the entered value is empty hence I'd use

if (nextProps.value && nextProps.value !== this.state.value) {

but I'm not sure if this would cover the common use cases 🤔

@gpbl gpbl changed the title When DayPickerInput get nextProps, it doesn't change states. When DayPickerInput get new props, it doesn't change its state May 25, 2017
@spmebrge
Copy link

+1 experiencing same issue

@gpbl gpbl closed this as completed in #365 May 25, 2017
@gpbl
Copy link
Owner

gpbl commented May 25, 2017

Thanks people for the report, the bug should have been fixed in v5.5.2

@youreme
Copy link
Author

youreme commented May 26, 2017

Hi~ thanks for update~!
it works well~

I have a question about componentDidMount.
why do you set state directly not using "setState" method?

thanks.

@gpbl
Copy link
Owner

gpbl commented May 26, 2017

@youreme are you talking about the constructor? https://stackoverflow.com/questions/34961853

@youreme
Copy link
Author

youreme commented May 28, 2017

@gpbl yes, constructor^^; Thanks for kindly information~

@darasi
Copy link

darasi commented Aug 16, 2019

I had a bug where if you type something in input and prop value changes it does not update input value.
I fixed it like this.

class DayInput extends DayPickerInput {
  handleInputBlur(e) {
    this.setState({ typedValue: undefined });
    this.inputBlurTimeout = setTimeout(() => {
      if (!this.overlayHasFocus) {
        this.hideDayPicker();
      }
    }, 1);
    if (this.props.inputProps.onBlur) {
      e.persist();
      this.props.inputProps.onBlur(e);
    }
  }
}

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

5 participants