From fcac3909b02f7e6730e0b3dd1f7c01ee4d365600 Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Sat, 13 Feb 2021 09:49:06 +0100 Subject: [PATCH] Add memoization --- .../controller/details/useCreateContext.tsx | 18 +++++++++++------- .../src/controller/details/useEditContext.tsx | 17 +++++++++++------ .../src/controller/details/useShowContext.tsx | 17 +++++++++++------ .../ra-core/src/controller/useListContext.ts | 16 ++++++++++------ 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/packages/ra-core/src/controller/details/useCreateContext.tsx b/packages/ra-core/src/controller/details/useCreateContext.tsx index 130325e7e1d..e8299188d8f 100644 --- a/packages/ra-core/src/controller/details/useCreateContext.tsx +++ b/packages/ra-core/src/controller/details/useCreateContext.tsx @@ -1,4 +1,4 @@ -import { useContext } from 'react'; +import { useContext, useMemo } from 'react'; import merge from 'lodash/merge'; import { Record } from '../../types'; @@ -32,12 +32,16 @@ export const useCreateContext = < // @ts-ignore CreateContext ); - // Props take precedence over the context - // @ts-ignore - return props != null - ? merge({}, context, extractCreateContextProps(props)) - : context; + return useMemo( + () => + merge( + {}, + context, + props != null ? extractCreateContextProps(props) : {} + ), + [context, props] + ); }; /** @@ -65,7 +69,7 @@ const extractCreateContextProps = ({ saving, successMessage, version, -}) => ({ +}: any) => ({ basePath, record, defaultTitle, diff --git a/packages/ra-core/src/controller/details/useEditContext.tsx b/packages/ra-core/src/controller/details/useEditContext.tsx index f8bc8096423..b811e7927b3 100644 --- a/packages/ra-core/src/controller/details/useEditContext.tsx +++ b/packages/ra-core/src/controller/details/useEditContext.tsx @@ -1,4 +1,4 @@ -import { useContext } from 'react'; +import { useContext, useMemo } from 'react'; import merge from 'lodash/merge'; import { Record } from '../../types'; @@ -30,10 +30,15 @@ export const useEditContext = ( const context = useContext>(EditContext); // Props take precedence over the context - // @ts-ignore - return props != null - ? merge({}, context, extractEditContextProps(props)) - : context; + return useMemo( + () => + merge( + {}, + context, + props != null ? extractEditContextProps(props) : {} + ), + [context, props] + ); }; /** @@ -62,7 +67,7 @@ const extractEditContextProps = ({ saving, successMessage, version, -}) => ({ +}: any) => ({ basePath, // Necessary for actions (EditActions) which expect a data prop containing the record // @deprecated - to be removed in 4.0d diff --git a/packages/ra-core/src/controller/details/useShowContext.tsx b/packages/ra-core/src/controller/details/useShowContext.tsx index 1ebf02b5170..fe615c70abc 100644 --- a/packages/ra-core/src/controller/details/useShowContext.tsx +++ b/packages/ra-core/src/controller/details/useShowContext.tsx @@ -1,4 +1,4 @@ -import { useContext } from 'react'; +import { useContext, useMemo } from 'react'; import merge from 'lodash/merge'; import { Record } from '../../types'; @@ -30,10 +30,15 @@ export const useShowContext = ( const context = useContext>(ShowContext); // Props take precedence over the context - // @ts-ignore - return props != null - ? merge({}, context, extractShowContextProps(props)) - : context; + return useMemo( + () => + merge( + {}, + context, + props != null ? extractShowContextProps(props) : {} + ), + [context, props] + ); }; /** @@ -51,7 +56,7 @@ const extractShowContextProps = ({ loading, resource, version, -}) => ({ +}: any) => ({ basePath, record, defaultTitle, diff --git a/packages/ra-core/src/controller/useListContext.ts b/packages/ra-core/src/controller/useListContext.ts index 84df8e87eee..4e7b8601ecc 100644 --- a/packages/ra-core/src/controller/useListContext.ts +++ b/packages/ra-core/src/controller/useListContext.ts @@ -1,4 +1,4 @@ -import { useContext } from 'react'; +import { useContext, useMemo } from 'react'; import merge from 'lodash/merge'; import ListContext from './ListContext'; @@ -97,12 +97,16 @@ const useListContext = ( props?: any ): ListControllerProps => { const context = useContext(ListContext); - // Props take precedence over the context - // @ts-ignore - return props != null - ? merge({}, context, extractListContextProps(props)) - : context; + return useMemo( + () => + merge( + {}, + context, + props != null ? extractListContextProps(props) : {} + ), + [context, props] + ); }; export default useListContext;