-
Notifications
You must be signed in to change notification settings - Fork 20
[Minor] Adding componentWillReceieveProps function #40
base: master
Are you sure you want to change the base?
[Minor] Adding componentWillReceieveProps function #40
Conversation
- When the defaultValue for the radon select is passed via a prop into radon-select from a higher-order parent component, radon-select does not reflect this changes. - This was because, the internal state of radon-select was never re-set. - This PR fixes that by adding a componetWillReceieveProps to update the internalState when defaultValue changes - Also added a simple example to illustrate this in demo page
src/select.jsx
Outdated
| : -1; | ||
| }, | ||
| getInitialState () { | ||
| var initialIndex = this._getInitialIndex(this.props.defaultValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You aren't using this value. Should it be used for selectedOptionIndex here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, I can't read.
| // This is a workaround for a long-standing iOS/React issue with click events. | ||
| // See https://github.com/facebook/react/issues/134 for more information. | ||
| , onTouchStart: this.tap, | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
LGTM. |
|
I've found updating internal state based on props like this to be a bug-prone pattern, personally. For this case it's a pretty simply set of data you're keeping in sync so it's probably not a big deal. Generally though, I like to follow a pattern where you can either use internal state or have a controlled component using props, but not both at the same time. That might not make sense here because you have to deal with the Also, hi @niki4810! 👋 😄 |
|
@niki4810 Looks like we don't have CI set up here, but checking this out locally I see some Lint warnings & an error (some from this PR, and a few pre-existing). Can you run |
|
@exogen will do. |
|
If the user has made a selection and |
|
@exogen That was my main intention for this change. As in, if the defaultValue is supplied from a store via a higher order component, this component will update it self. I've noticed, that our dev's were adopting towards a |
|
@niki4810 Wouldn't the intention be to update the default if the user hasn't yet made a selection though? Why would the user's explicit choice change just because the default updated? |
|
@exogen The scenario we are working on is on |
|
@niki4810 I suppose if your |
Summary
componetWillReceievePropsto update the internalState when defaultValue changesScreenshot
Lint check
//cc @rgerstenberger @exogen @kenwheeler