-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
ArrowKeyStepper: setState(...) in componentWillUpdate
#589
Comments
You're right in pointing out that the component should not be calling I'm not sure I follow on your second paragraph. If props change, I assume the intent is for the prop to override the state. So what you're describing sounds like what I would expect to be desired? |
Hey Brian, Here's the working example: http://plnkr.co/edit/jgTqAr1CTZSil5Un5xKZ?p=preview I'm referencing the latest source of ArrowKeyStepper (with your fix), with this minor change: componentWillReceiveProps (nextProps) {
} The difference is that I'm reading Here's why: It'd be great if you could take a look and tell me if you got my point and if you think that's a correct approach (that would eventually require a code change) or not. Thx! |
The problem with using // First render
// User is initially scrolled to row 50 by default
<ArrowKeyStepper
columnCount={100}
rowCount={100}
scrollToRow={50}
/>
// User uses arrow keys to move down to row 100 at which point more rows load
// And we re-render with an updated row count (and no other props changes)
<ArrowKeyStepper
columnCount={100}
rowCount={200}
scrollToRow={50}
/> In the above example, if we compared Maybe the thing to do here is what I've done with other components and just add a public method for explicitly overriding this property (eg |
So you're basically saying to add a method in
Not sure how I could access an instance of the element in order to call this method. |
With a ref 😄 class MyComponent extends Component {
render () {
return (
<ArrowKeyStepper
{...this.props}
ref={this._setRef}
/>
)
}
_setRef (ref) {
// If you later need to use any public methods
// You can: this._arrowKeyStepperRef.somePublicMethod()
this._arrowKeyStepperRef = ref
}
}
We could potentially do that as well. |
I see! ^_^ Thanks for the help! |
No worries. I think that's a common thought-pattern about refs. 😄 Sure! I'd love to get a PR from you. Thanks for offering! |
Here! Please let me know if you're gonna complete the PR or if there's something else I need to do first! Thx! |
Resolved by #592 |
Hi,
not sure if this should be considered an issue, but I thought it would not hurt to talk about it.
In the
ArrowKeyStepper
component, into thecomponentWillUpdate
React lifecycle method, there is a call to setState(...).I am not entirely sure that's correct. (https://facebook.github.io/react/docs/react-component.html#componentwillupdate).
Isn't it a best practice to use
componentWillReceiveProps
instead?Also, I see that the state is updated if the new set of props differ from the old set of props.
But this way, if the component updates its state as a result of key down events and then it receives a new pair of values for
scrollToRow
andscrollToIndex
from a parent component (say, for example, as the result of clicking on one of the items of the wrapped List or Grid), those values will not end up in the state, and the component will not be able to reflect those in its render method.(if not clear, I'll try to come up with a small demo).
Thoughts?
The text was updated successfully, but these errors were encountered: