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

Do not crash when unsetting scrollToRow #62

Merged
merged 4 commits into from
Oct 11, 2016
Merged

Do not crash when unsetting scrollToRow #62

merged 4 commits into from
Oct 11, 2016

Conversation

IanVS
Copy link
Contributor

@IanVS IanVS commented Oct 10, 2016

Description

This avoids trying to scroll to an undefined row.

Motivation and Context

Since the latest release (0.7.5), I began to see the following error:

Uncaught Error: Invariant Violation: Index out of range undefined

I found that this happened after setting a scrollToRow prop and then returning it to undefined.

How Has This Been Tested?

I tested this in my app, as well as added a test for it. Note: I needed to be able to setState, which required full rendering, so I add jsdom and react-dom to the dev dependencies.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.

@@ -912,7 +912,7 @@ var FixedDataTable = React.createClass({
}

var lastScrollToRow = oldState ? oldState.scrollToRow : undefined;
if (props.scrollToRow !== lastScrollToRow) {
if (typeof props.scrollToRow !== 'undefined' && props.scrollToRow !== null && props.scrollToRow !== lastScrollToRow) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should introduce lodash into our lib and use _.isNil instead?
We wouldn't need to worry about bloat if we implemented tree shaking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be cool, but I guess I would argue that could be a separate PR after this one (if this is accepted).

@@ -912,7 +912,7 @@ var FixedDataTable = React.createClass({
}

var lastScrollToRow = oldState ? oldState.scrollToRow : undefined;
if (props.scrollToRow !== lastScrollToRow) {
if (typeof props.scrollToRow !== 'undefined' && props.scrollToRow !== null && props.scrollToRow !== lastScrollToRow) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to implement something similar for the other: scrollTop, scrollToRow and scrollToColumn

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too, but they weren't failing similar tests, while this one was, so I opted not to.

@KamranAsif KamranAsif self-assigned this Oct 10, 2016
@@ -0,0 +1,28 @@
'use strict'

var jsdom = require('jsdom')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing semicolons throughout this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I get for copying it from somewhere else. Sure would be nice to have ESLint set up for this project. Any interest in a PR doing so?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, would love it!

@IanVS
Copy link
Contributor Author

IanVS commented Oct 11, 2016

What will it take to get this merged? Add semicolons? Would you like checks around scrollTop, scrollLeft, etc?

@KamranAsif
Copy link
Contributor

@IanVS Just want to checkout the branch and test a couple edge cases. Are you in a hurry to get this merged in?

@IanVS
Copy link
Contributor Author

IanVS commented Oct 11, 2016

Nope, no particular hurry, just want to make sure I understand the expectations. Wasn't sure if you were done reviewing.

@KamranAsif
Copy link
Contributor

Added some extra tests; found scrollTop was also failing.
I refactored the tests to reuse your instance and added some improvements to make it more generic

@wcjordan Can you give my changes a look?

@@ -920,7 +920,7 @@ var FixedDataTable = React.createClass({
}

var lastScrollTop = oldState ? oldState.scrollTop : undefined;
if (props.scrollTop !== lastScrollTop) {
if (typeof props.scrollTop !== 'undefined' && props.scrollTop !== null && props.scrollTop !== lastScrollTop) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use typeof for this check? why not just props.scrollTop !== undefined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I just copied it. I think we can just do props.scrollTop != null since undefined == null
Will update this and above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a habit I suppose, but it's usually considered safer. In strict mode accessing a variable that is undefined will throw a referenceError. That may not apply in this case, but as a general rule to be safe that's how I like to check for undefined variables.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm that isn't my finding here: http://jsfiddle.net/s14sav6k/4/
But I'm not too familiar with strict mode. Do you have documentation which describes what can cause the ReferenceErrors? Could it be a browser specific implementation?

Copy link
Contributor Author

@IanVS IanVS Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate pointing to SO as a source of truth, but here's this: http://stackoverflow.com/questions/2703102/typeof-undefined-vs-null

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll get that error for variable reference, but not for property reference.
e.g. props.foo won't throw that error, as long as props is defined

Copy link
Member

@wcjordan wcjordan Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I think the case you're referring to is specific to references on the execution context (props for this case), rather than accessed on an object through dot notation (e.g. props.scrollTop).

If there's a risk of props not existing, we'd need to also add a typeof props !== undefined && check to avoid an error from the dot access. If not, I think we're safe with the short hand Kam suggests in both strict and non-strict modes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, just a habit. ;)

* @return {!Object}
*/
getTableState() {
return this.refs['table'].state;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the motivation for this state access indirection? might be worth adding a comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use it to inspect the inner state for the tests. I'll update the annotation to further clarify

@wcjordan
Copy link
Member

I added a few questions, but lgtm

@KamranAsif KamranAsif merged commit 79db986 into schrodinger:master Oct 11, 2016
@IanVS IanVS deleted the fix-scrollToRow branch October 11, 2016 17:11
@IanVS
Copy link
Contributor Author

IanVS commented Oct 11, 2016

Thanks everyone.

@KamranAsif
Copy link
Contributor

Published in 0.7.6

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

Successfully merging this pull request may close these issues.

None yet

3 participants