Skip to content

Commit

Permalink
Add memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Feb 13, 2021
1 parent 88ae477 commit fcac390
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
18 changes: 11 additions & 7 deletions packages/ra-core/src/controller/details/useCreateContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react';
import { useContext, useMemo } from 'react';
import merge from 'lodash/merge';

import { Record } from '../../types';
Expand Down Expand Up @@ -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]
);
};

/**
Expand Down Expand Up @@ -65,7 +69,7 @@ const extractCreateContextProps = ({
saving,
successMessage,
version,
}) => ({
}: any) => ({
basePath,
record,
defaultTitle,
Expand Down
17 changes: 11 additions & 6 deletions packages/ra-core/src/controller/details/useEditContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react';
import { useContext, useMemo } from 'react';
import merge from 'lodash/merge';

import { Record } from '../../types';
Expand Down Expand Up @@ -30,10 +30,15 @@ export const useEditContext = <RecordType extends Record = Record>(
const context = useContext<EditControllerProps<RecordType>>(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]
);
};

/**
Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions packages/ra-core/src/controller/details/useShowContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react';
import { useContext, useMemo } from 'react';
import merge from 'lodash/merge';

import { Record } from '../../types';
Expand Down Expand Up @@ -30,10 +30,15 @@ export const useShowContext = <RecordType extends Record = Record>(
const context = useContext<ShowControllerProps<RecordType>>(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]
);
};

/**
Expand All @@ -51,7 +56,7 @@ const extractShowContextProps = ({
loading,
resource,
version,
}) => ({
}: any) => ({
basePath,
record,
defaultTitle,
Expand Down
16 changes: 10 additions & 6 deletions packages/ra-core/src/controller/useListContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react';
import { useContext, useMemo } from 'react';
import merge from 'lodash/merge';

import ListContext from './ListContext';
Expand Down Expand Up @@ -97,12 +97,16 @@ const useListContext = <RecordType extends Record = Record>(
props?: any
): ListControllerProps<RecordType> => {
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;
Expand Down

0 comments on commit fcac390

Please sign in to comment.