Skip to content

Conversation

@piotrski
Copy link
Contributor

@piotrski piotrski commented Nov 1, 2024

Summary

This PR adds support for displaying the names of changed hooks directly in the Profiler tab, making it easier to identify specific updates.

A HookChangeSummary component has been introduced to show these hook names, with a displayMode prop that toggles between “compact” for tooltips and “detailed” for more in-depth views. This keeps tooltip summaries concise while allowing for a full breakdown where needed.

This functionality also respects the “Always parse hook names from source” setting from the Component inspector, as it uses the same caching mechanism already in place for the Components tab. Additionally, even without hook names parsed, the Profiler will now display hook types (like State, Callback, etc.) based on data from inspectedElement.

To enable this across the DevTools, InspectedElementContext has been moved higher in the component tree, allowing it to be shared between the Profiler and Components tabs. This update allows hook name data to be reused across tabs without duplication.

Additionally, a getAlreadyLoadedHookNames helper function was added to efficiently access cached hook names, reducing the need for repeated fetching when displaying changes.

These changes improve the ability to track specific hook updates within the Profiler tab, making it clearer to see what’s changed.

Before

Previously, the Profiler tab displayed only the IDs of changed hooks, as shown below:
Screenshot 2024-11-01 at 12 02 21_cropped

After (without hook names parsed)

When hook names aren’t parsed, custom hooks and hook types are displayed based on the inspectedElement data:
Screenshot 2024-11-01 at 12 03 09_cropped

After (with hook names parsed)

Once hook names are fully parsed, the Profiler tab provides a complete breakdown of specific hooks that have changed:
Screenshot 2024-11-01 at 12 03 14_cropped

This should resolve #21856 🎉

@vercel
Copy link

vercel bot commented Nov 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 12, 2024 2:00pm

@hoxyq
Copy link
Contributor

hoxyq commented Nov 13, 2024

Hey, thanks for upstreaming this, really appreciate any contributions to React DevTools.

I am currently on leave and will be back first week of December. This change will probably require some manual testing on my side, so I think its better to iterate on this once I am fully available.

Comment on lines 101 to 127
const [parseHookNamesOptimistic, setParseHookNamesOptimistic] =
useState(parseHookNames);

useEffect(() => {
setParseHookNamesOptimistic(parseHookNames);
}, [inspectedElement?.id, parseHookNames]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks strange, you are keeping something from a global context in a local state, and then reconciliate it here? Why parseHookNames from InspectedElementContext is not enough?

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 approach is something I borrowed from the Inspected Element Panel here.

The idea behind the parseHookNamesOptimistic state is to keep the UI responsive. Since changing parseHookNames happens in a transition (which can suspend), this local state ensures the toggle feels immediate while the actual state updates in the background.

I considered making this logic reusable, by extracting LoadHookNamesButton into its own component, but I didn’t want to optimize prematurely. Do you think it’s worth revisiting?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, okay, we should probably refactor this to use useOptimistic in the future.

Comment on lines 123 to 126
const hookParsingFailed = useMemo(
() => parseHookNames && hookNames === null,
[parseHookNames, hookNames],
);
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably don't need useMemo for this one

Copy link
Contributor

@hoxyq hoxyq left a comment

Choose a reason for hiding this comment

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

Looks good overall, please address the comment that I've left.

I am planning to manually test this next week, and if everything works as expected, we can merge this

Comment on lines 83 to 85
return (
hook.subHooks?.some(subHook => shouldKeepHook(subHook, hooksArray)) ?? false
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return (
hook.subHooks?.some(subHook => shouldKeepHook(subHook, hooksArray)) ?? false
);
const subHooks = hook.subHooks;
if (subHooks == null) {
return false;
}
return subHooks.some(subHook => shouldKeepHook(subHook, hooksArray));

Comment on lines 96 to 106
if (hook.subHooks?.length > 0) {
const filteredSubHooks = hook.subHooks
.map(subHook => filterHooks(subHook, hooksArray))
.filter(Boolean);

return filteredSubHooks.length > 0
? {...hook, subHooks: filteredSubHooks}
: {...hook};
}

return hook;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (hook.subHooks?.length > 0) {
const filteredSubHooks = hook.subHooks
.map(subHook => filterHooks(subHook, hooksArray))
.filter(Boolean);
return filteredSubHooks.length > 0
? {...hook, subHooks: filteredSubHooks}
: {...hook};
}
return hook;
const subHooks = hook.subHooks;
if (subHooks == null) {
return hook;
}
const filteredSubHooks = subHooks
.map(subHook => filterHooks(subHook, hooksArray))
.filter(Boolean);
return filteredSubHooks.length > 0
? {...hook, subHooks: filteredSubHooks}
: hook;

</span>
{hook.subHooks?.map((subHook, index) => (
<Hook
key={`${hook.id}-${index}`}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you really need an index here for constructing a key? hook.id and index actually have a correlation, because both of them depend on the fact when the hook was defined relatively to other hooks: in the end all of them are going to be stored in a linked list.

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 remember it was a dirty fix to prevent stale hook info, but I couldn't reproduce it now, so I removed it.

@corentin-gautier
Copy link

Can this be moved forward ?

@hoxyq
Copy link
Contributor

hoxyq commented Mar 5, 2025

@piotrski Are you planning to continue the work on this or are you okay with someone picking this up?

@piotrski
Copy link
Contributor Author

piotrski commented Mar 5, 2025

@hoxyq sorry, I was sick, and then I got caught up with other things and completely missed this. I’ll try to get it done this week since I finally have some time for OSS

@hoxyq
Copy link
Contributor

hoxyq commented Mar 5, 2025

@hoxyq sorry, I was sick, and then I got caught up with other things and completely missed this. I’ll try to get it done this week since I finally have some time for OSS

No worries, thank you!

…cache

The InspectedElementContext was previously only available in the Components tab.
This change moves the context provider to a higher level in the component
hierarchy, making the InspectedElementContext available in both the Components
and Profiler tabs.
- Introduced `HookChangeSummary` component to show names of changed hooks, improving clarity and making it easier to identify specific hook updates in the Profiler tab.
- Added `getAlreadyLoadedHookNames` helper for efficient retrieval of hook names.

Resolves facebook#21856
…ry in tooltips

- Added `displayMode` prop to `HookChangeSummary` for `detailed` and `compact` views.
- Updated `WhatChanged` and `HoveredFiberInfo` to use `compact` mode in tooltips, preserving the previous concise summary display.

This change maintains the original hook change summary style in tooltips while allowing a detailed view where needed.
@piotrski
Copy link
Contributor Author

@hoxyq I’ve addressed all the comments, let me know if anything else needs further adjustments. Looking forward to your feedback.
I’d like to add a diff of the hook values, but I didn’t want this PR to grow too much, so I think it’s better to do it in a separate PR. I’m not sure about the UI for that yet.
Also, hook name parsing is a bit flaky, sometimes it fails silently, but I haven’t been able to figure out why.

@hoxyq
Copy link
Contributor

hoxyq commented Mar 17, 2025

I’d like to add a diff of the hook values, but I didn’t want this PR to grow too much, so I think it’s better to do it in a separate PR.

Makes sense, that would be awesome.

Also, hook name parsing is a bit flaky, sometimes it fails silently, but I haven’t been able to figure out why.

Yeah, the implementation is flawed, and was based on a lot of assumptions about the source code, but that's what we have. This should not block you from landing this change, since you are just using it as an API.

Copy link
Contributor

@hoxyq hoxyq left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for working on this! I will run few manual tests later this week and merge it if everything is alright.

@hoxyq
Copy link
Contributor

hoxyq commented Mar 31, 2025

Hey, just circling back here to say that this is still in my queue.

In my scenario, I end up in a weird state where I can see the button for fetching hook names on the inspected element panel, but not on the profile tab, I am only seeing hooks indexes. We should probably avoid doing this. I am not sure yet what is going wrong, though.

Will follow up here once I have more details.

@piotrski
Copy link
Contributor Author

piotrski commented Apr 1, 2025

@hoxyq thanks for the update! Happy to jump on a quick call if that helps speed up debugging, just let me know.

Copy link
Contributor

@hoxyq hoxyq left a comment

Choose a reason for hiding this comment

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

All good with changes in this diff, thanks for contributing. I am going to proceed with merging this.

Before the next release, I will try to find some time to fix the hook names parsing implementation, because right now it is mostly broken for Flow and extremely slow for larger apps. I don't have an approximal date for next release yet.

@hoxyq hoxyq merged commit 7ff4d05 into facebook:main Apr 15, 2025
194 checks passed
@piotrski
Copy link
Contributor Author

Thanks for merging! Yeah, I’ve noticed the hook name parsing can be a bit flaky too, especially in Next.js projects. If there's anything I can do to help with that, just let me know. I'm happy to help.

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.

[DevTools] Show which hooks (names) changed in the Profiler

5 participants