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

Add ReactDebugIntrospection #6488

Closed
wants to merge 2 commits into from
Closed

Add ReactDebugIntrospection #6488

wants to merge 2 commits into from

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Apr 11, 2016

Note: this is stacked on top of #6486 so ignore the first commit.


This is a follow-up to #6486, also extracted from #6046.

Here, we add an introspection API that will let React DevTools, ReactPerf, and third party tools query information about the internal instances without exposing them.

This is a follow-up to #6389, also extracted from #6046.

In #6046, we added a new API for associating internal instances with debug-time IDs. Third-party tools such as React DevTools and ReactPerf would use those IDs to query information about the instances, rather than use the instances directly.

Here, we add the integration of `ReactDebugInstanceMap` into `ReactDOM` and `ReactDOMServer` rendering. The instances are registered during instantiation because we need to know their IDs right away: some events, such as “we are calling the constructor”, may fire before the instance is mounted. Currently we unregister instances when they get unmounted but in the future it will be possible to separate unmounting from unregistration, in case we ever want to support reparenting.

One notable change here is that we now call `unmountComponent()` for server rendering in `__DEV__`. Traditionally this has not been necessary, as server rendering code path in mounting doesn’t allocate any resources it needs to release. However, now that we track every component instantiation, we also want to remove this information, so this requires a real unmounting pass on the server. Nevertheless, since this change is only relevant for `__DEV__`, it does not affect the production performance.

Some existing code in `unmountComponent()` used to rely on the fact that it’s only called on the client. To mitigate this, I added a flag called `hasReactMountReady` to the transactions. It indicates whether `getReactMountReady()` is a real queue or a no-op. This way, component can tell, for example, whether it needs to run `componentWillUnmount()`, or whether it needs to ensure the node is allowed to be unmounted, such as in case of `<html>`.

One concern I have is that conditionally running `unmountComponent()` for server rendering can make it easy to introduce accidental memory leaks, as it will always run in the test environment but not in production. An alternative option would be to add a separate method called `releaseComponent()` that serves as `unmountComponent()` during server rendering only. This way relying on `unmountComponent()` deallocating a resource will be easy to spot because `unmountComponent()` still won’t run in tests for server rendering.

Another concern is that we’re finally using `ReactDebugInstanceMap` in the code which means we now have a dependency on WeakMap being available in `__DEV__` builds. If this is bad, we should do something about it.
This is a follow-up to #6486, also extracted from #6046.

Here, we add an introspection API that will let React DevTools, ReactPerf, and third party tools query information about the internal instances without exposing them.
@omerts
Copy link

omerts commented Apr 13, 2016

I would be happy to hear what people think about making the API two way, meaning adding ability to inject back into the component things such as state, and props. That way devtools could also allow to temper with the components, and see how they change.
We are working on being able to replay inner component states, with the redux-devtools extension, and the above api could really help us avoid dirtier solutions.

@gaearon
Copy link
Collaborator Author

gaearon commented Apr 13, 2016

I would be happy to hear what people think about making the API two way, meaning adding ability to inject back into the component things such as state, and props.

Yea, this will definitely be added at a later stage after new ReactPerf is ready. We want to migrate React DevTools to use this API, and they have a way of tweaking props so scheduling updates will be allowed here as well.

@gaearon
Copy link
Collaborator Author

gaearon commented Apr 19, 2016

Closing per #6486 (comment). Will do it differently.

@gaearon gaearon closed this Apr 19, 2016
@gaearon gaearon deleted the reactperf-introspection-api branch April 25, 2016 16:47
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.

3 participants