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

useEffect cleanup triggering multiple times after updates? #503

Closed
akbr opened this issue May 30, 2023 · 2 comments
Closed

useEffect cleanup triggering multiple times after updates? #503

akbr opened this issue May 30, 2023 · 2 comments

Comments

@akbr
Copy link

akbr commented May 30, 2023

While trying to troubleshoot this issue, I noticed that hmr updates seem to be triggering redundant useEffect cleanup calls.

import { useState, useEffect } from 'preact/hooks';

export function App() {
  const [count, setCount] = useState(0);
  const inc = () => setCount((x) => x + 1);

  return <InnerApp count={count} inc={inc} />;
}

function InnerApp({ count, inc }) {
  useEffect(() => {
    console.log('Effect run');
    return () => console.log('Effect cleanup');
  });

  return (
    <div>
      <div>Count is: {count}</div>
      <button onClick={inc}>Increment</button>
    </div>
  );
}

If I click "Increment" 20 times, and then trigger an HMR update, the useEffect cleanup will run 20 (additional) times:

image

Sandbox: https://stackblitz.com/edit/vitejs-vite-dxlaxr?file=src%2Fapp.jsx&terminal=dev

@JoviDeCroock
Copy link
Member

Closed by #504 and #505

@akbr
Copy link
Author

akbr commented May 31, 2023

Closed by #504 and #505

Amazing — thank you! A huge boost to DX when editing on a large preact project.

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 a pull request may close this issue.

2 participants