Skip to content

Conversation

@bfiss
Copy link
Contributor

@bfiss bfiss commented Sep 20, 2017

Addressing issue #8 . Thanks @gaearon for the reviews.

This only exposes:

  1. a ref, same that would be provided by React when using ReactDOM.render(...) and refs in general;
  2. a file name where that ref is defined.

@wmertens
Copy link

Will this allow inspecting the ref to see what context names it's subscribed to? If not, that would be useful information to expose as well.

@bfiss
Copy link
Contributor Author

bfiss commented Sep 21, 2017

@wmertens is that information available in this.context within a React instance? If so, then yes, it has that as well as props, state and any instance properties. Otherwise, it might be available in this.constructor, which should also be available for class instances. You can check it out by executing ReactDOM.render(...) on a node that is subscribed to context names, and seeing if what's returned contains what you are looking for.

@wmertens
Copy link

I am not sure, this is the approach I took before: https://github.com/gaearon/react-deep-force-update/pull/4/files#diff-1fdf421c05c1140f6d71444ea2b27638R25

Basically, if the contextName is a function on the class's contextTypes, that would be the correct test, but not sure if we can get at that information via ref?

@bfiss
Copy link
Contributor Author

bfiss commented Sep 21, 2017

@wmertens, I see from your code that you were using internalInstance._instance.context[contextName], and ref is in fact internalInstance._instance (on stack-based React), so the use case is definitely still possible.

src/index.js Outdated
let node = root
while (true) {
if (node.tag === ReactClassComponent && shouldUpdate(node)) {
const publicInfo = extractPublicInfoFiber(node);
Copy link
Owner

Choose a reason for hiding this comment

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

Seems unfortunate to call it if it's not used. I guess this is the point where I ask you to change it back to check the function existence inline (and lazily calculate this if either function exists). Sorry!

src/index.js Outdated
const fileName = internalInstance._debugSource &&
internalInstance._debugSource.fileName;
return {
ref: internalInstance._instance,
Copy link
Owner

Choose a reason for hiding this comment

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

Let's rename this field to publicInstance. The instance doesn't really have a ref and it can be a bit confusing to overload this term.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(y)

src/index.js Outdated

function extractPublicInfoFiber(internalInstance) {
const fileName = internalInstance.child &&
internalInstance.child._debugSource &&
Copy link
Owner

Choose a reason for hiding this comment

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

Why are you reading filename from the child rather than from the component itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I could see while playing around in Power Editor, if I'm looking for an instance of component Foo, defined in foo.js, It will be Foo's children who will have _debugSource.fileName === 'foo.js'. I couldn't test this with stack-based React since it seems it's not that easy to turn that on for Facebook code anymore, but for Fiber, it's working really well in practice.

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe, I just don't understand why. This doesn't really make sense to me: a child is the child component, not the current component. Maybe you could give an example of component I could look at where children have filenames but parents don't?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We talked about this offline. Seems like we can use .displayName, which isn't always available, or use this heuristic, or something else I haven't thought of. Here's what's usually available:

screen shot 2017-09-21 at 3 23 47 am

@bfiss
Copy link
Contributor Author

bfiss commented Sep 21, 2017

Sounds good, will update soon.

@bfiss bfiss force-pushed the master branch 15 times, most recently from 3de9de3 to d14d98f Compare September 24, 2017 02:15
@bfiss
Copy link
Contributor Author

bfiss commented Sep 24, 2017

This removes support for shouldUpdate and onUpdate for stack-based React. We should make sure to address #10.

@gregberge
Copy link
Collaborator

@bfiss you should add test on Fiber if possible

@bfiss bfiss force-pushed the master branch 2 times, most recently from 46ef848 to e2a60b3 Compare October 2, 2017 23:53
@bfiss
Copy link
Contributor Author

bfiss commented Oct 2, 2017

Talked with @gaearon and we decided to upgrade React to 16. This will automatically get the tests on Fiber.

@bfiss
Copy link
Contributor Author

bfiss commented Oct 2, 2017

There's a new warning, but I guess it should be okay to ignore:

Warning: React depends on requestAnimationFrame. Make sure that you load a polyfill in older browsers. http://fb.me/react-polyfills

@chabou
Copy link

chabou commented Nov 6, 2017

Thank you so much for your work 🙏
I really need to have this PR merged.
Any ETA?

@gregberge
Copy link
Collaborator

I didn't follow it, @bfiss @gaearon any update?

@bfiss
Copy link
Contributor Author

bfiss commented Nov 7, 2017

@neoziro @chabou @gaearon this is being used extensively within Facebook, which is on Fiber. This was just waiting for the tests to actually run on Fiber, it should be good to go since the tests are running on Fiber now.

@gregberge
Copy link
Collaborator

@gaearon @bfiss even I think we should document shouldUpdateClosestClassInstance and onUpdateClassInstance in the readme. Also it is a breaking change. We should release it under a v3 right?

Copy link
Owner

@gaearon gaearon left a comment

Choose a reason for hiding this comment

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

We'll need to avoid reading the tag field. This means both changing the shouldUpdate callback to not take it, and changing the class check to something like

  const hasInstance =
    node.stateNode !== null &&
    typeof node.type === 'function';

(I already upstreamed this change on www)

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.

5 participants