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

Fixed component double rendering in StrictMode #310

Merged
merged 3 commits into from
May 18, 2022
Merged

Fixed component double rendering in StrictMode #310

merged 3 commits into from
May 18, 2022

Conversation

psmyrek
Copy link
Contributor

@psmyrek psmyrek commented May 18, 2022

Suggested merge commit message (convention)

Fix: Fixed component double rendering in StrictMode. Closes #294.


Additional information

It looks like this issue exists only in React v18+. React strict mode checks are run in development mode only. They do not impact the production build. For testing purposes we can use the sample/index.html.

  1. Checkout the i/294 branch, bump React version in package.json for react and react-dom to ^18.0.0 and install dependencies.

  2. Load the development builds for React framework in the sample/index.html:

    - <script src="../node_modules/react/umd/react.production.min.js"></script>
    - <script src="../node_modules/react-dom/umd/react-dom.production.min.js"></script>
    + <script src="../node_modules/react/umd/react.development.js"></script>
    + <script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
  3. Wrap the sample application in the <React.StrictMode>. Also, you need to change the way how the component is rendered in the sample/index.html (as the ReactDOM.render() is no longer supported in React v18):

    - document.addEventListener( 'DOMContentLoaded', () => {
    -     ReactDOM.render(
    -         React.createElement( App ),
    -         document.getElementById( 'root' )
    -     );
    - } );
    + document.addEventListener( 'DOMContentLoaded', () => {
    +     ReactDOM
    +         .createRoot( document.getElementById( 'root' ) )
    +         .render( React.createElement( React.StrictMode, null, React.createElement( App ) ) );
    + } );
  4. Build the component: yarn run develop.

  5. Open the sample and make sure it is rendered only once. There should be no warnings or errors in the console. If you have React Developer Tools installed in your browser, in the Components tab there should be no red exclamation sign indicating that the app is not in strict mode (because it is).

@coveralls
Copy link

coveralls commented May 18, 2022

Pull Request Test Coverage Report for Build 423

  • 14 of 14 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 418: 0.0%
Covered Lines: 183
Relevant Lines: 183

💛 - Coveralls

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.

Investigate double-rendering in React StrictMode
3 participants