Skip to content

Commit

Permalink
Fix table re-renders on update field (#6722)
Browse files Browse the repository at this point in the history
The update of all fields was caused by `useContextSelector` not being
properly implemented.

As it is a memoization library the `useRecordFieldValue` hook wasn't
giving to the library the required things to allow memoization.

I just added recordId + fieldName to the memoization function so that
`useContextSelector` doesn't recompute itself whenever any record
changes.
  • Loading branch information
lucasbordeau authored Aug 23, 2024
1 parent e49acae commit 6467da5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ export const useSetRecordValue = () => {
export const useRecordValue = (recordId: string) => {
const tableValue = useContextSelector(
RecordFieldValueSelectorContext,
(value) => value[0],
(value) => value[0]?.[recordId],
);

return tableValue?.[recordId] as ObjectRecord | undefined;
return tableValue as ObjectRecord | undefined;
};

export const useRecordFieldValue = <T,>(
recordId: string,
fieldName: string,
) => {
const recordFieldValues = useContextSelector(
const recordFieldValue = useContextSelector(
RecordFieldValueSelectorContext,
(value) => value[0],
(value) => value[0]?.[recordId]?.[fieldName],
);

return recordFieldValues?.[recordId]?.[fieldName] as T;
return recordFieldValue as T | undefined;
};

export const useSetRecordFieldValue = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const validateEmail = (email: string) => {
};

type EmailDisplayProps = {
value: string | null;
value: string | null | undefined;
};

export const EmailDisplay = ({ value }: EmailDisplayProps) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatNumber } from '~/utils/format/number';
import { EllipsisDisplay } from './EllipsisDisplay';

type NumberDisplayProps = {
value: string | number | null;
value: string | number | null | undefined;
};

export const NumberDisplay = ({ value }: NumberDisplayProps) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MouseEvent } from 'react';
import { parsePhoneNumber, PhoneNumber } from 'libphonenumber-js';
import { MouseEvent } from 'react';

import { ContactLink } from '@/ui/navigation/link/components/ContactLink';
import { isDefined } from '~/utils/isDefined';

type PhoneDisplayProps = {
value: string | null;
value: string | null | undefined;
};

// TODO: see if we can find a faster way to format the phone number
Expand Down

0 comments on commit 6467da5

Please sign in to comment.