Skip to content

Fix react strict mode issue#1435

Merged
patricvincit merged 3 commits into
mainfrom
react-strict-mode-issue
Jun 2, 2026
Merged

Fix react strict mode issue#1435
patricvincit merged 3 commits into
mainfrom
react-strict-mode-issue

Conversation

@patricvincit
Copy link
Copy Markdown
Contributor

@patricvincit patricvincit commented May 28, 2026

Enabling strict mode caused 6 tests to fail.

stopDetails.cy.ts

  • 'should view stop area basic details text fields'

Failed because the form was marked as dirty on strict mode which caused a confirmation modal to open. This happened because reason for change was not initialised on the form.

  • 'should be able to keep shelter numbers in order'

This failed on strict mode because shelter numbering was done in useEffect so strict mode caused it to give wrong shelter numbers as a side effect.

createRoute.cy.ts

  • 'Should create a new route',
  • 'Should create a new route and leave out one stop'
  • 'Should not let the user create a route with only one stop'
  • 'Should create new route with an indefinite validity end date'

All failed because cypress started clicking on the map before draw layer was actually ready. Fixed by adding a loader on the route creation while the drawing is not ready.


This change is Reviewable

@patricvincit patricvincit marked this pull request as draft May 28, 2026 11:56
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@patricvincit patricvincit force-pushed the react-strict-mode-issue branch from 9454f60 to 4a202c8 Compare June 1, 2026 08:06
@patricvincit patricvincit marked this pull request as ready for review June 1, 2026 09:43
Copy link
Copy Markdown
Contributor

@Huulivoide Huulivoide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Huulivoide reviewed 6 files and all commit messages, and made 3 comments.
Reviewable status: 6 of 7 files reviewed, 3 unresolved discussions (waiting on patricvincit).


ui/src/components/map/routes/DrawRouteLayer.tsx line 169 at r1 (raw file):

  }, [keyDown]);

  const isDrawingReady = useCallback(

Funktion nimi pikasesti implikois että tää on funktio joka tekee tarkastuksia ja sylkee ulos jonkin booleanin, vaikka oikeasti tää tekee "Set loading state based on given args". Tätä kutsutaan myös vaan yhdessä paikassa, niin sanosin että tästä voi ottaa ton "isReady" osuuden ulos ja siirtä tiedostoon ylätasolle function isDrawingReady(...): boolean.

Ja sit tuolla useFffectissä:
setLoadingState(isDrawingRead() ? a : b)


ui/src/components/map/routes/DrawRouteLayer.tsx line 171 at r1 (raw file):

  const isDrawingReady = useCallback(
    (mapInstance: MapInstance, drawReference: MapboxDraw | null) => {
      const hasDrawRef = Boolean(drawReference);

Käsin koodatussa koodissa on käytetty !!-operaattoria boolean kästinä


ui/src/components/stop-registry/stops/stop-details/shelters/useSheltersForm.tsx line 18 at r1 (raw file):

    .filter((num): num is number => num !== null && !Number.isNaN(num));

  return (usedNumbers.length ? Math.max(...usedNumbers) : 0) + 1;

Math.max(0, ...usedNumbers) + 1

The basic details form became dirty right after opening edit mode in react strict mode.

This happened because reasonForChange started as undefined, but the textarea used an empty string.
React Hook Form saw that as a change and marked the form dirty.

Set reasonForChange default to empty string so the form starts clean and no false unsaved-changes warning appears.
Shelter numbers could jump in Strict Mode because numbering was done in a component effect.

Move number assignment to add/copy actions in the shelters form utilities.
Now each new shelter gets its number only when the user adds or copies, so numbering stays predictable.
Prevent route creation clicks before draw mode is fully initialised.
@patricvincit patricvincit force-pushed the react-strict-mode-issue branch from 4a202c8 to ee5ce43 Compare June 2, 2026 05:32
Copy link
Copy Markdown
Contributor Author

@patricvincit patricvincit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patricvincit made 3 comments.
Reviewable status: 3 of 7 files reviewed, 3 unresolved discussions (waiting on Huulivoide).


ui/src/components/map/routes/DrawRouteLayer.tsx line 169 at r1 (raw file):

Previously, Huulivoide (Jesse Jaara) wrote…

Funktion nimi pikasesti implikois että tää on funktio joka tekee tarkastuksia ja sylkee ulos jonkin booleanin, vaikka oikeasti tää tekee "Set loading state based on given args". Tätä kutsutaan myös vaan yhdessä paikassa, niin sanosin että tästä voi ottaa ton "isReady" osuuden ulos ja siirtä tiedostoon ylätasolle function isDrawingReady(...): boolean.

Ja sit tuolla useFffectissä:
setLoadingState(isDrawingRead() ? a : b)

Tässä tää callback funktio aiheuttikin ongelmia routen editoinnissa. Antoi väärää tietoa valmiudesta ja testit kaatui kun klikkailtiin taas liian aikaisin (oli eri testit kuin alkuperäiset mitä korjailin niin jäi huomaamatta). Kevensin nyt tota rakennetta vähäsen samassa, enää ei ole erikseen tota aiempaa handleDrawReadyä, joka kutsuu sitten tota toista funktiota, vaan siirsin suoraan handleDrawReadyyn sen valmiuden käsittelyn (ja koitin nimetä selkeemmäksi).

Ja tiputin const { current: mapboxDraw } = drawRef; alustamisen pois, kun 95% komponentista käytti suoraan drawRef.current, niin pysyy sitten yhtenäisenä


ui/src/components/map/routes/DrawRouteLayer.tsx line 171 at r1 (raw file):

Previously, Huulivoide (Jesse Jaara) wrote…

Käsin koodatussa koodissa on käytetty !!-operaattoria boolean kästinä

Done.


ui/src/components/stop-registry/stops/stop-details/shelters/useSheltersForm.tsx line 18 at r1 (raw file):

Previously, Huulivoide (Jesse Jaara) wrote…

Math.max(0, ...usedNumbers) + 1

Done.

Copy link
Copy Markdown
Contributor

@Huulivoide Huulivoide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Huulivoide reviewed 4 files and all commit messages, and resolved 3 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on patricvincit).

@patricvincit patricvincit merged commit 47e2af0 into main Jun 2, 2026
27 of 28 checks passed
@patricvincit patricvincit deleted the react-strict-mode-issue branch June 2, 2026 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants