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

[core] fix(HotkeysProvider): avoid unnecessary children re-renders #6643

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/core/src/context/hotkeys/hotkeysProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type HotkeysAction =
export type HotkeysContextInstance = [HotkeysContextState, React.Dispatch<HotkeysAction>];

const initialHotkeysState: HotkeysContextState = { hasProvider: false, hotkeys: [], isDialogOpen: false };
// const initialHotkeysState: HotkeysContextState = { hasProvider: false };
const noOpDispatch: React.Dispatch<HotkeysAction> = () => null;

/**
Expand Down Expand Up @@ -122,9 +123,14 @@ export const HotkeysProvider = ({ children, dialogProps, renderDialog, value }:
/>
);

const reducedState = React.useMemo<HotkeysContextState>(
() => ({ hasProvider: state.hasProvider }),
[state.hasProvider],
);

// if we are working with an existing context, we don't need to generate our own dialog
return (
<HotkeysContext.Provider value={[state, dispatch]}>
<HotkeysContext.Provider value={[reducedState, dispatch]}>
Copy link
Contributor

@braeden braeden Jan 11, 2024

Choose a reason for hiding this comment

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

(totally aware this is a draft) But I think we actually want to memo this reference itself too. eg.

const memoValue = React.useMemo(() => [reducedState, dispatch], [])

Also have seen this show up in some perf-profiles, but haven't had the time to follow-up. Without a memo, each time this provider renders, it also re-renders every single consuming child (even if state/dispatch) haven't changed.

Edit: #6652

{children}
{hasExistingContext ? undefined : dialog}
</HotkeysContext.Provider>
Expand Down