ui-components icon indicating copy to clipboard operation
ui-components copied to clipboard

fix(#2413): react 19 upgrade

Open BumbleB2na opened this issue 1 year ago • 6 comments

Run npm ci or npm install

Before (the change)

Could not use react 19 in a project when importing @abgov/react-components without a peer dependency conflict.

After (the change)

Should no longer encounter a peer dependency conflict when using react 19 in a project that imports @abgov/react-components. An upgrade to React 19 was required.

Upgrade to React 19:

  1. followed react 19 upgrade guide: https://react.dev/blog/2024/04/25/react-19-upgrade-guide
  2. upgraded 4 packages to react version 19.0.0:
    npm install --save-exact react@^19.0.0 react-dom@^19.0.0 @types/react@^19.0.0 @types/react-dom@^19.0.0
    
  3. ran codemon for react
    npx codemod@latest react/19/migration-recipe
    
    • no changes
  4. ran codemon for react typescript:
    npx types-react-codemod@latest preset-19 ./_templates/react
    
    • it changed "ref" objects in .tsx files (in React 19 "useRef" works more like "useContext"). Some manual changes were needed, like in form.tsx for example:
      • ref?: React.MutableRefObject<HTMLElement | undefined>; --> ref?: React.RefObject<HTMLElement | null>; because MutableRefObject is deprecated.
      • const el = useRef<HTMLElement>(); --> const el = useRef<HTMLElement>(null); because initializing RefObject is required and undefined is disallowed.
    • See React 19 Upgrade guide: https://react.dev/blog/2024/04/25/react-19-upgrade-guide#useref-requires-argument
  5. attempted build, then had to manually changed each react component that defines a custom element to be used in its return statement. Take form.tsx for example:
    • before the change it would throw an error during `nx run react-components:build:production': "error TS2339: Property 'goa-simple-form' does not exist on type 'JSX.IntrinsicElements'."
    • The error and solution are somewhat similar to what is described here: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/71395#discussioncomment-11525908
    • See React 19 Upgrade guide: https://react.dev/blog/2024/04/25/react-19-upgrade-guide#the-jsx-namespace-in-typescript
  6. needed to change all boolean values in react components to convert to string when passed to related web component.
    • Changed boolean in a consistent way so that react components do not set a default, allowing props for setting boolean values to optional.
    • Updated all related tests to
      • render a component with only required value and check boolean flags to make sure they're optional
      • render a component with non-required properties and set any boolean flags to true to make sure they can be set
  7. built without errors, served react playground
  8. upgraded libs/react-components/package.json: allow peer dependencies for react ^19.0.0
    • Should no longer encounter a peer dependency conflict when using react 19 in a project that imports @abgov/react-components.

Steps to test

  • run npm ci to upgrade locally and confirm there are no peer dependency conflicts
  • optional: could try each component in react playground and toggle settings related to changed boolean flags (although there's test coverage for all of the changed boolean flags)

BumbleB2na avatar Mar 06 '25 21:03 BumbleB2na

@BumbleB2na Can you solve the conflict of this PR so I can do QA?

vanessatran-ddi avatar Mar 27 '25 21:03 vanessatran-ddi

27th March 2025, I tested with React 18, and React 19. Both works well.

vanessatran-ddi avatar Mar 27 '25 23:03 vanessatran-ddi

@chrisolsen formatting changes were moved to a separate commit

BumbleB2na avatar Apr 03 '25 20:04 BumbleB2na

@BumbleB2na Can you rebase this PR?

chrisolsen avatar Apr 22 '25 19:04 chrisolsen

@BumbleB2na Thanks for rebasing, unfortunately after some other merges it is out of sync again :)

chrisolsen avatar Apr 23 '25 16:04 chrisolsen

@chrisolsen rebased and ready for re-review. See this comment: https://github.com/GovAlta/ui-components/pull/2460#discussion_r2056727047

BumbleB2na avatar Apr 23 '25 19:04 BumbleB2na

:tada: This PR is included in version 1.33.0-alpha.15 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

tzuge avatar Apr 28 '25 17:04 tzuge

:tada: This PR is included in version 6.3.0-alpha.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

tzuge avatar Apr 28 '25 17:04 tzuge

:tada: This PR is included in version 4.3.0-alpha.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

tzuge avatar May 01 '25 14:05 tzuge

:tada: This PR is included in version 1.3.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

tzuge avatar May 01 '25 19:05 tzuge

:tada: This PR is included in version 4.3.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

tzuge avatar May 01 '25 19:05 tzuge

:tada: This PR is included in version 1.33.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

tzuge avatar May 01 '25 19:05 tzuge

:tada: This PR is included in version 6.3.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

tzuge avatar May 01 '25 19:05 tzuge