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] memoize context values for react/jsx-no-constructed-context-values #34849

Merged
merged 5 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
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
17 changes: 10 additions & 7 deletions docs/data/base/components/menu/UseMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,16 @@ const Menu = React.forwardRef(function Menu(props, ref) {
open,
});

const contextValue = {
registerItem,
unregisterItem,
getItemState,
getItemProps,
open: true,
};
const contextValue = React.useMemo(
() => ({
registerItem,
unregisterItem,
getItemState,
getItemProps,
open: true,
}),
[getItemProps, getItemState, registerItem, unregisterItem],
);

return (
<ul className="menu-root" {...other} {...getListboxProps()}>
Expand Down
17 changes: 10 additions & 7 deletions docs/data/base/components/menu/UseMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,16 @@ const Menu = React.forwardRef(function Menu(
open,
});

const contextValue: MenuUnstyledContextType = {
registerItem,
unregisterItem,
getItemState,
getItemProps,
open: true,
};
const contextValue: MenuUnstyledContextType = React.useMemo(
() => ({
registerItem,
unregisterItem,
getItemState,
getItemProps,
open: true,
}),
[getItemProps, getItemState, registerItem, unregisterItem],
);

return (
<ul className="menu-root" {...other} {...getListboxProps()}>
Expand Down
7 changes: 6 additions & 1 deletion docs/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ function AppWrapper(props) {
];
}

const pageContextValue = React.useMemo(
() => ({ activePage, pages: productPages }),
[activePage, productPages],
);

return (
<React.Fragment>
<NextHead>
Expand All @@ -219,7 +224,7 @@ function AppWrapper(props) {
<LanguageNegotiation />
<CodeCopyProvider>
<CodeVariantProvider>
<PageContext.Provider value={{ activePage, pages: productPages }}>
<PageContext.Provider value={pageContextValue}>
<ThemeProvider>
<DocsStyledEngineProvider cacheLtr={emotionCache}>
{children}
Expand Down
13 changes: 9 additions & 4 deletions examples/remix-with-typescript/app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ interface ClientCacheProviderProps {
function ClientCacheProvider({ children }: ClientCacheProviderProps) {
const [cache, setCache] = useState(createEmotionCache());

function reset() {
setCache(createEmotionCache());
}
const clientStyleContextValue = React.useMemo(
() => ({
reset() {
setCache(createEmotionCache());
},
}),
[],
);

return (
<ClientStyleContext.Provider value={{ reset }}>
<ClientStyleContext.Provider value={clientStyleContextValue}>
<CacheProvider value={cache}>{children}</CacheProvider>
</ClientStyleContext.Provider>
);
Expand Down
40 changes: 20 additions & 20 deletions packages/mui-base/src/FormControlUnstyled/FormControlUnstyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,26 @@ const FormControlUnstyled = React.forwardRef(function FormControlUnstyled<
required,
};

const handleChange = (event: React.ChangeEvent<NativeFormControlElement>) => {
setValue(event.target.value);
onChange?.(event);
};

const childContext: FormControlUnstyledState = {
disabled,
error,
filled,
focused,
onBlur: () => {
setFocused(false);
},
onChange: handleChange,
onFocus: () => {
setFocused(true);
},
required,
value: value ?? '',
};
const childContext: FormControlUnstyledState = React.useMemo(() => {
return {
disabled,
error,
filled,
focused,
onBlur: () => {
setFocused(false);
},
onChange: (event: React.ChangeEvent<NativeFormControlElement>) => {
setValue(event.target.value);
onChange?.(event);
},
onFocus: () => {
setFocused(true);
},
required,
value: value ?? '',
};
}, [disabled, error, filled, focused, onChange, required, setValue, value]);

const classes = useUtilityClasses(ownerState);

Expand Down
17 changes: 10 additions & 7 deletions packages/mui-base/src/MenuUnstyled/MenuUnstyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ const MenuUnstyled = React.forwardRef(function MenuUnstyled<
className: classes.listbox,
});

const contextValue: MenuUnstyledContextType = {
registerItem,
unregisterItem,
getItemState,
getItemProps,
open,
};
const contextValue: MenuUnstyledContextType = React.useMemo(
() => ({
registerItem,
unregisterItem,
getItemState,
getItemProps,
open,
}),
[getItemProps, getItemState, open, registerItem, unregisterItem],
);

return (
<Root {...rootProps}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,14 @@ const MultiSelectUnstyled = React.forwardRef(function MultiSelectUnstyled<TValue
className: classes.popper,
});

const context: SelectUnstyledContextType = {
getOptionProps,
getOptionState,
listboxRef,
};
const context: SelectUnstyledContextType = React.useMemo(
() => ({
getOptionProps,
getOptionState,
listboxRef,
}),
[getOptionProps, getOptionState],
);

return (
<React.Fragment>
Expand Down
13 changes: 8 additions & 5 deletions packages/mui-base/src/SelectUnstyled/SelectUnstyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,14 @@ const SelectUnstyled = React.forwardRef(function SelectUnstyled<TValue extends {
className: classes.popper,
});

const context: SelectUnstyledContextType = {
getOptionProps,
getOptionState,
listboxRef,
};
const context: SelectUnstyledContextType = React.useMemo(
() => ({
getOptionProps,
getOptionState,
listboxRef,
}),
[getOptionProps, getOptionState],
);

return (
<React.Fragment>
Expand Down
17 changes: 10 additions & 7 deletions packages/mui-joy/src/AvatarGroup/AvatarGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ const AvatarGroup = React.forwardRef(function AvatarGroup(inProps, ref) {

const { className, color, component = 'div', size = 'md', variant, children, ...other } = props;

const ownerState = {
...props,
color,
component,
size,
variant,
};
const ownerState = React.useMemo(
() => ({
...props,
color,
component,
size,
variant,
}),
[color, component, props, size, variant],
);

const classes = useUtilityClasses();

Expand Down
7 changes: 6 additions & 1 deletion packages/mui-joy/src/Chip/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,13 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
className: classes.endDecorator,
});

const chipContextValue = React.useMemo(
() => ({ disabled, variant, color }),
[color, disabled, variant],
);

return (
<ChipContext.Provider value={{ disabled, variant, color }}>
<ChipContext.Provider value={chipContextValue}>
<ChipRoot
as={component}
className={clsx(classes.root, className)}
Expand Down
33 changes: 17 additions & 16 deletions packages/mui-joy/src/FormControl/FormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
} = props;

const id = useId(idOverride);
const labelId = `${id}-label`;
const helperTextId = `${id}-helper-text`;
const [helperText, setHelperText] = React.useState<HTMLElement | null>(null);

const ownerState = {
Expand Down Expand Up @@ -117,21 +115,24 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {

const classes = useUtilityClasses(ownerState);

const formControlContextValue = React.useMemo(
() => ({
disabled,
required,
error,
color,
size,
htmlFor: id,
labelId: `${id}-label`,
'aria-describedby': helperText ? `${id}-helper-text` : undefined,
setHelperText,
registerEffect: registerEffect!,
}),
[color, disabled, error, helperText, id, registerEffect, required, size],
);

return (
<FormControlContext.Provider
value={{
disabled,
required,
error,
color,
size,
htmlFor: id,
labelId,
'aria-describedby': helperText ? helperTextId : undefined,
setHelperText,
registerEffect: registerEffect!,
}}
>
<FormControlContext.Provider value={formControlContextValue}>
<FormControlRoot
as={component}
ownerState={ownerState}
Expand Down
17 changes: 10 additions & 7 deletions packages/mui-joy/src/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,16 @@ const Menu = React.forwardRef(function Menu(inProps, ref) {
ownerState,
});

const contextValue: MenuUnstyledContextType = {
registerItem,
unregisterItem,
getItemState,
getItemProps,
open,
};
const contextValue: MenuUnstyledContextType = React.useMemo(
() => ({
registerItem,
unregisterItem,
getItemState,
getItemProps,
open,
}),
[getItemProps, getItemState, open, registerItem, unregisterItem],
);

return (
<PopperUnstyled {...rootProps}>
Expand Down
20 changes: 12 additions & 8 deletions packages/mui-joy/src/MenuList/MenuList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ const MenuList = React.forwardRef(function MenuList(inProps, ref) {
className: classes.root,
});

const contextValue = {
registerItem,
unregisterItem,
getItemState,
getItemProps,
getListboxProps,
open: true,
} as MenuUnstyledContextType;
const contextValue = React.useMemo(
() =>
({
registerItem,
unregisterItem,
getItemState,
getItemProps,
getListboxProps,
open: true,
} as MenuUnstyledContextType),
[getItemProps, getItemState, getListboxProps, registerItem, unregisterItem],
);

return (
<MenuListRoot {...listboxProps}>
Expand Down
4 changes: 3 additions & 1 deletion packages/mui-joy/src/ModalDialog/ModalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ const ModalDialog = React.forwardRef(function ModalDialog(inProps, ref) {

const classes = useUtilityClasses(ownerState);

const contextValue = React.useMemo(() => ({ variant, color }), [color, variant]);

return (
<ModalDialogSizeContext.Provider value={size}>
<ModalDialogVariantColorContext.Provider value={{ variant, color }}>
<ModalDialogVariantColorContext.Provider value={contextValue}>
<ModalDialogRoot
as={component}
ownerState={ownerState}
Expand Down
Loading