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

Type ReactRef #7600

Merged
merged 1 commit into from Sep 2, 2016
Merged

Type ReactRef #7600

merged 1 commit into from Sep 2, 2016

Conversation

vjeux
Copy link
Contributor

@vjeux vjeux commented Aug 29, 2016

Nothing out of the ordinary on this one.

@@ -23,7 +23,7 @@ export type ReactElement = {
$$typeof: any,
type: any,
key: any,
ref: any,
ref: string | (elem: ?HTMLElement) => void,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Expanding the types of ReactElement as I go along. Flow coverage tool in nuclide is super useful to see what flows sees in the code I flowify :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any way flow coverage could be integrated into flow CLI? Many people don't use Nuclide and when they try flow, they aren't aware flow coverage exists.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(I understand there's a separate command but it's easy to miss when you are learning or trying it for the first time. I didn't know it existed until someone mentioned it on Twitter a month ago.)

Copy link
Contributor Author

@vjeux vjeux Aug 29, 2016

Choose a reason for hiding this comment

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

There's already a command flow coverage <file> but it just shows the summary which isn't really useful:

screen shot 2016-08-29 at 9 04 24 am

Would be awesome if it printed in ascii art what nuclide shows:

screen shot 2016-08-29 at 9 02 43 am

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

flow coverage --color
On Mon, 29 Aug 2016 at 19:04, Christopher Chedeau notifications@github.com
wrote:

In src/isomorphic/classic/element/ReactElementType.js
#7600 (comment):

@@ -23,7 +23,7 @@ export type ReactElement = {
$$typeof: any,
type: any,
key: any,

  • ref: any,
  • ref: string | (elem: ?HTMLElement) => void,

cc @thejameskyle https://github.com/thejameskyle


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/facebook/react/pull/7600/files/6a1166f44d77d267879a9e39b6cde9241a569043#r76634323,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAB3gk_ZuNizzgRHP7SeOrxVSKUMDotlks5qkwMFgaJpZM4JvD5_
.

Copy link
Contributor Author

@vjeux vjeux Aug 29, 2016

Choose a reason for hiding this comment

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

Omg, @andreypopp it works!

screen shot 2016-08-29 at 9 54 23 am

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why isn’t it the default 😄

Copy link
Member

Choose a reason for hiding this comment

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

(elem: ?HTMLElement) => void isn't right - you can get a component instance passed in (in the case of refs on composite components).

I think we probably want to define this correctly as much as possible and not leave partially correct types on fields (probably fine to leave any on to-be type fields).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(elem: ?HTMLElement) => void isn't right - you can get a component instance passed in (in the case of refs on composite components).

Good catch! I need to figure out what the type of a component instance is.

I think we probably want to define this correctly as much as possible and not leave partially correct types on fields (probably fine to leave any on to-be type fields).

Agreed, I want anything to be typed to be fully correct.

@@ -56,20 +66,21 @@ ReactRef.shouldUpdateRefs = function(prevElement, nextElement) {
// is made. It probably belongs where the key checking and
// instantiateReactComponent is done.

var prevEmpty = prevElement === null || prevElement === false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

flow is not smart enough to know that this check is flowing through the variable for the next condition

@@ -56,20 +66,21 @@ ReactRef.shouldUpdateRefs = function(prevElement, nextElement) {
// is made. It probably belongs where the key checking and
// instantiateReactComponent is done.

var prevEmpty = prevElement === null || prevElement === false;
var nextEmpty = nextElement === null || nextElement === false;

return (
// This has a few false positives w/r/t empty components.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be nice to learn what those false positives are since we're touching this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This blames to 999b0f9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cc @spicyj, let me know if you have more context on this/any concern.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the idea is that if both prevElement == null || prevElement === false and nextElement == null || nextElement === false then the function will return true. This is a false positive because if it was empty and stays empty, there is no need to update refs. Probably not a big deal.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that's right.

@vjeux
Copy link
Contributor Author

vjeux commented Aug 30, 2016

(Added needs-revision based on @zpao feedback)

@vjeux
Copy link
Contributor Author

vjeux commented Aug 30, 2016

Removed the ref type on ReactInstanceType as we don't yet have a type for the user class and it wasn't accurate enough.

Typed the function as ReactInstance | null | false in order to ensure that undefined cannot slip in.

I'm looking for another review, thanks for the comments!

ReactRef.detachRefs = function(instance, element) {
ReactRef.detachRefs = function(
instance: ReactInstance,
element: ReactElement,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be null or false here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I can't wait for flow to warn when you're doing checks that do not make sense based on the type you have.

@vjeux
Copy link
Contributor Author

vjeux commented Aug 30, 2016

Added null | false in detachRef

Nothing out of the ordinary on this one.
// If owner changes but we have an unchanged function ref, don't update refs
(typeof nextElement.ref === 'string' &&
nextElement._owner !== prevElement._owner)
(typeof nextRef === 'string' && nextOwner !== prevOwner)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the tests are passing but i'd love a second pair of eyes to know if this is safe

@vjeux
Copy link
Contributor Author

vjeux commented Sep 2, 2016

It turned out to be a meatier change when adding string, false and null to the mix :)

@gaearon
Copy link
Collaborator

gaearon commented Sep 2, 2016

Looks right to me. If it turns out to be wrong but we didn’t have tests for that case, our fault (and we’ll add tests).

@vjeux vjeux added this to the 15-next milestone Sep 2, 2016
@vjeux vjeux merged commit 31dd694 into facebook:master Sep 2, 2016
acdlite pushed a commit to acdlite/react that referenced this pull request Sep 9, 2016
Nothing out of the ordinary on this one.
@zpao zpao modified the milestones: 15-next, 15.4.0 Oct 4, 2016
zpao pushed a commit that referenced this pull request Oct 4, 2016
Nothing out of the ordinary on this one.
(cherry picked from commit 31dd694)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants