Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react 18): upgrade to react 18. #7336

Merged
merged 47 commits into from
Jun 14, 2024

Conversation

v-rakeshsh
Copy link
Contributor

@v-rakeshsh v-rakeshsh commented May 15, 2024

Details

This feature updates below packages.

  1. react from v16 to v18.
  2. react-dom from v16 to v18.
  3. @types-react from v16 to v18.
  4. @types-react-dom from v16 to v18.
  5. @testing-library/react from v12 to v15.
  6. @fluentui/react from v8.x.x to v8.118.1.
  7. Removed react-helmet and added react-helmet-async.

1. Notable changes for react, react-dom:

Motivation: React 18 introduces a new root API which provides better ergonomics for managing roots. The new root API also enables the new concurrent renderer, which allows you to opt-into concurrent features.

In V16, we had below to render the component:
import { render } from 'react-dom';
const container = document.getElementById('app');
render(, container);

  • In V18, we have below to render the component:
    import { createRoot } from 'react-dom/client';
    const container = document.getElementById('app');
    const root = createRoot(container); // createRoot(container!) if you use TypeScript
    root.render();

2. Notable changes for @types-react and @types-react-dom:

Motivation: The new types are safer and catch issues that used to be ignored by the type checker. The most notable change is that the children prop now needs to be listed explicitly when defining props

  • In old we have below
    WrappedComponent: React.ComponentType

    ,

  • In new we have below
    WrappedComponent: React.ComponentType<React.PropsWithChildren

    >,

Approach for type changes: So this Type changes are added using automation script https://github.com/eps1lon/types-react-codemod. This automation script is suggested in react18 migration document.

  • Added new package types-react-codemod.
  • After adding the package, executed yarn types-react-codemod preset-18 ./src in root, and then selected all option from the list of options.
  • This will transform all types of component type having child components to <React.PropsWithChildren

    >.

3. Notable changes for @testing-library/react:

4. Notable changes for @fluentui/react from v8.x.x to v8.118.1

  • Existing fluent ui version does not support react18, test cases were failing, hence after checking v8.118.1 documentation, it supports react and react-dom v18. Hence upadated.

5. Notable changes for react-helmet-async:

  • Current react-helmet package throws error 'objects cannot be child, expected elements', for react18, Hence as alternative used react-helmet-async. For reference https://www.npmjs.com/package/react-helmet-async?activeTab=readme because react-helmet-async uses react18 as dependency.
  • Wrapped Helmet provider for root, as to pass context of react-helmet-async.
  • Created a variable to store data, and then this data was passed as JSX, instead of passing the data as it is. Because it will throw "Objects cannot be used as react elements".

For example:
export const GuidanceTitle = NamedFC<GuidanceTitleProps>('GuidanceTitle', ({ name }) => { const titleValue = Guidance for ${name} - ${productName}; return ( <> <Helmet> <title>{titleValue}</title> </Helmet> <h1>{name}</h1> </> ); });

6. Along with above

  • Made changes to mock helpers, because after react18 changes, the JSON structure of component was coming differently, so accordingly corrected the helpers, to get proper component name for snapshots.
  • Updated snapshots, because as we are using latest Fluent UI version, new props are introduced which can be seen in snapshots.
  • Refactored few test cases, which were wrong logically, like for example:
    using of mockReactComponents in global and inside test case using of useOriginalComponents to get the props using getMockComponentClassPropsForCall which was wrong logically is fixed to use any one approach.
  • Updated report package with react, react-dom v18 to keep in sync with AI web.
Context

This PR includes all changes required for migration of AI web from react16 to react18.
It includes test cases fixes.
It includes lint issues fixes.

Pull request checklist

  • Addresses an existing issue: #0000
  • Ran yarn fastpass
  • Added/updated relevant unit test(s) (and ran yarn test)
  • Verified code coverage for the changes made. Check coverage report at: <rootDir>/test-results/unit/coverage
  • PR title AND final merge commit title both start with a semantic tag (fix:, chore:, feat(feature-name):, refactor:). See CONTRIBUTING.md.
  • (UI changes only) Added screenshots/GIFs to description above
  • (UI changes only) Verified usability with NVDA/JAWS

@v-rakeshsh v-rakeshsh requested a review from a team as a code owner May 15, 2024 11:26
@v-rakeshsh v-rakeshsh marked this pull request as draft May 15, 2024 11:26
@v-viyada v-viyada marked this pull request as ready for review May 24, 2024 16:38
@v-viyada v-viyada changed the title V rakeshsh/react18 migration feat(react 18): upgrade to react 18. May 24, 2024
Copy link
Contributor

@madalynrose madalynrose left a comment

Choose a reason for hiding this comment

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

This PR is a huge effort! Well done, team! I have some questions about changes, mostly in tests.

@v-viyada v-viyada merged commit 9ee52e9 into microsoft:main Jun 14, 2024
12 of 13 checks passed
v-viyada pushed a commit that referenced this pull request Jun 19, 2024
…accessibility insights ui (#7370)

#### Details

Update package version for accessibility insights report and
accessibility insights ui for React 18 upgrade. Please refer #7336
Note : Making as major version update. On local testing we found that it
will bring a breaking change as application consuming these packages
also needs to update react and react-dom to v18 to avoid type mismatch.

##### Motivation

<!-- This can be as simple as "addresses issue #123" -->

##### Context

<!-- Are there any parts that you've intentionally left out-of-scope for
a later PR to handle? -->

<!-- Were there any alternative approaches you considered? What
tradeoffs did you consider? -->

#### Pull request checklist
<!-- If a checklist item is not applicable to this change, write "n/a"
in the checkbox -->
- [na] Addresses an existing issue: #0000
- [x] Ran `yarn fastpass`
- [na] Added/updated relevant unit test(s) (and ran `yarn test`)
- [x] Verified code coverage for the changes made. Check coverage report
at: `<rootDir>/test-results/unit/coverage`
- [x] PR title *AND* final merge commit title both start with a semantic
tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`). See
`CONTRIBUTING.md`.
- [na] (UI changes only) Added screenshots/GIFs to description above
- [na] (UI changes only) Verified usability with NVDA/JAWS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants