Skip to content

Commit

Permalink
use react's useId in place of generateSourceId
Browse files Browse the repository at this point in the history
it does not change when the component is rerendered
  • Loading branch information
sndrs committed May 24, 2024
1 parent e0c9fba commit b3202ef
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/client/components/CheckboxInput.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';
import React, { useId } from 'react';
import type { EmotionJSX } from '@emotion/react/types/jsx-namespace';
import { Checkbox } from '@guardian/source-react-components';
import { SerializedStyles, css } from '@emotion/react';
import {
textSans,
focusHalo,
descriptionId,
generateSourceId,
space,
palette,
textSans12,
Expand Down Expand Up @@ -126,7 +125,7 @@ export const CheckboxInput = ({
cssOverrides,
onToggle,
}: CheckboxInputProps): EmotionJSX.Element => {
const switchName = id ?? generateSourceId();
const switchName = id ?? useId();

Check failure on line 128 in src/client/components/CheckboxInput.tsx

View workflow job for this annotation

GitHub Actions / Continuous Integration

React Hook "useId" is called conditionally. React Hooks must be called in the exact same order in every component render
const labelId = descriptionId(switchName);

return (
Expand Down
5 changes: 2 additions & 3 deletions src/client/components/ToggleSwitchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useId } from 'react';
import type { EmotionJSX } from '@emotion/react/types/jsx-namespace';
import type { Props } from '@guardian/source-react-components';
import { css } from '@emotion/react';
Expand All @@ -9,7 +9,6 @@ import {
focusHalo,
visuallyHidden,
descriptionId,
generateSourceId,
space,
} from '@guardian/source-foundations';

Expand Down Expand Up @@ -174,7 +173,7 @@ export const ToggleSwitchInput = ({
imagePath,
cssOverrides,
}: ToggleSwitchInputProps): EmotionJSX.Element => {
const switchName = id ?? generateSourceId();
const switchName = id ?? useId();

Check failure on line 176 in src/client/components/ToggleSwitchInput.tsx

View workflow job for this annotation

GitHub Actions / Continuous Integration

React Hook "useId" is called conditionally. React Hooks must be called in the exact same order in every component render
const labelId = descriptionId(switchName);

return (
Expand Down

0 comments on commit b3202ef

Please sign in to comment.