Skip to content

Commit

Permalink
Memoize clues label (#1861)
Browse files Browse the repository at this point in the history
## What are you changing?

- Memoize clues label

## Why?

- save calling every render
  • Loading branch information
sndrs authored Dec 18, 2024
2 parents d70174d + d5e6d2b commit 3dfa225
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions libs/@guardian/react-crossword/src/components/Clues.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { css } from '@emotion/react';
import type { ComponentType, ReactNode } from 'react';
import { useCallback, useEffect, useRef } from 'react';
import { memo, useCallback, useEffect, useRef } from 'react';
import type { Direction } from '../@types/Direction';
import type { EntryID } from '../@types/Entry';
import { useCurrentCell } from '../context/CurrentCell';
Expand All @@ -21,6 +21,22 @@ type Props = {
}>;
};

const Label = memo(({ direction }: { direction: Direction }) => {
const { getId } = useData();

return (
<label
css={css`
color: currentColor;
`}
id={getId(`${direction}-label`)}
htmlFor={getId(`${direction}-hints`)}
>
{direction}
</label>
);
});

export const Clues = ({ direction, Header }: Props) => {
const { entries, getId } = useData();
const { progress } = useProgress();
Expand Down Expand Up @@ -65,21 +81,15 @@ export const Clues = ({ direction, Header }: Props) => {
}
}

const label = (
<label
css={css`
color: currentColor;
`}
id={getId(`${direction}-label`)}
htmlFor={getId(`${direction}-hints`)}
>
{direction}
</label>
);

return (
<div ref={cluesRef}>
{Header ? <Header>{label}</Header> : label}
{Header ? (
<Header>
<Label direction={direction} />
</Header>
) : (
<Label direction={direction} />
)}

<div
tabIndex={0}
Expand Down

0 comments on commit 3dfa225

Please sign in to comment.