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

[React18] useLayoutEffect runs multiple times while using ReactDOMClient.createRoot #29

Open
CJY0208 opened this issue Nov 11, 2022 · 0 comments

Comments

@CJY0208
Copy link

CJY0208 commented Nov 11, 2022

Problem

useLayoutEffect runs multiple times while using ReactDOMClient.createRoot

Open in StackBlitz

test

Reproduce Code

import * as React from 'react';
import { render } from 'react-dom';
import { createRoot } from 'react-dom/client';
import { Freeze } from 'react-freeze';

const rootElement = document.getElementById('root')
render(<App />, rootElement); // √
createRoot(rootElement).render(<App />); // x

function Test() {
  React.useLayoutEffect(() => {
    console.log('run useLayoutEffect'); // Log is executed for each unfreezing
  }, []);

  return <div>test</div>;
}

function App() {
  const [freeze, setFreeze] = React.useState(false);

  return (
    <div>
      <button onClick={() => setFreeze(!freeze)}>toggle freeze state</button>
      <Freeze freeze={freeze}>
        <Test />
      </Freeze>
    </div>
  );
}
@CJY0208 CJY0208 changed the title useLayoutEffect runs multiple times while using ReactDOMClient.createRoot [React18] useLayoutEffect runs multiple times while using ReactDOMClient.createRoot Nov 11, 2022
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

No branches or pull requests

1 participant