-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Expose devtools hook injection to other consumers on React object #2797
Comments
One of the reasons we added it was because React may not load immediately and many tools wants to be notified once React is loaded an available. There might also be multiple Reacts on a single page or at least in multiple iframes. The idea is that other tools can override and chain the |
Chaining would be cool but it doesn't work now this way, does it? Once DevTools hook ran, you can't have your inject called somehow; moreover DevTools define it as non-writeable. Any way we can change hook API to explicitly support chaining? |
Never mind! |
Monkey patching internals will break as soon as we start precompiling React using Closure Compiler so you'll need officially supported hooks. Just like the devtools and ReactPerf etc. will also need.
|
No, I'm not talking about monkeypatching React internals. I'm monkeypatching user's components. Because they pass through my transform anyway, in addition to method forwarding, I hijack componentWillMount and componentWillUnmount to track instances. This proves to be enough in my case. Also, this is only going to be enabled in development, so I suspect in this case I'm getting a “source” React and not precompiled one anyway. |
@gaearon Earlier just this morning I put together what I believe @sebmarkbage was talking about with the chaining. For my particular use-case I'm using your On a personal note, I really didn't like what I had to do to make this work, since the powers that be seem to be opposed to exposing https://gist.github.com/timbur/5317a6079a585db253a4 I suppose this might be a "solution" to your source problem, but I agree that in the future we will probably need something more elegant like you described in your original post. |
@timbur Have you looked at react-hotify yet? It has a more succinct API than react-hot-api and does not require you to pass |
How do you solve the problem of per-component This is made more explicit in react-hotify API: it keeps an internal map of “hotifying” functions per component class, and you are required to pass a string to it uniquely identifying the component. What usually works fairly well is filename plus component name. |
@gaearon Thanks for the heads up about each class needing their own I just checked out Upon first glance it looks like it would be pretty easy to make
Semi-unrelated, regarding that: Anyone know if |
I think it's still slightly wrong because those
If you implement it, can I ask you to PR it to react-hotify? Ideally I'd like it to be more of a team effort than it is now :-). It's got plenty of tests so they should give you an idea of how to test the API. It would be sensible to add a test case using PS Maybe you could open an issue in react-hotify so we don't spam people here. |
What REACT_DEVTOOLS_GLOBAL_HOOK.inject method does? |
@skirankumar7 You may find these two source files helpful for understanding what data the hook injection site makes available:
|
It's four years later; is there some sort of API available now for third party extensions to use the same I notice here that when react-devtools places its hook at that site, it marks the property as non-configurable: https://github.com/facebook/react/blob/master/packages/react-devtools-shared/src/hook.js#L305 This is unfortunate as it means no other extensions are able place their own patched/intercept version of the hook there. Granted, there is a workaround: while developers of other extensions can't patch/intercept the hook variable itself, we can patch all the properties on the hook object. We can do something like:
Is the above the best way currently available, or is there a newer way I'm not aware of? EDIT Complete example for others: (works regardless of which extension runs first)
|
Currently React looks for
__REACT_DEVTOOLS_GLOBAL_HOOK__
global and callsinject
on it, if it exists. This works fine if there's one listener (e.g. Chrome React DevTools themselves).I'd love it if React supported arbitrary number of independent hooks. In React Hot Loader I'm reaching into internals (I need
ReactMount
to get all mounted instances) but I wish I could do that withoutrequire
-ing an internal module.Ideally I wish React exposed the same modules that are available to global hooks, via some kind of
React.registerDeveloperTool()
. This would be called automatically for__REACT_DEVTOOLS_GLOBAL_HOOK__
but available onReact
object if I want to do it from a library. (I don't mean production libraries, but debugging aid libraries such as React Hot Loader and potentially others.)The text was updated successfully, but these errors were encountered: