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 inside refreshed code runs needlessly #808

Closed
foucdeg opened this issue Mar 13, 2024 · 1 comment
Closed

useEffect inside refreshed code runs needlessly #808

foucdeg opened this issue Mar 13, 2024 · 1 comment

Comments

@foucdeg
Copy link

foucdeg commented Mar 13, 2024

This is a component with a "mock" useEffect that tracks changes to variables.

function MyComponent() {
  const variable = "hello"
  const oldValueRef = useRef()

   useEffect(() => {
    console.log("variable changed", oldValueRef.current, variable)
    oldValueRef.current = variable
   }, [variable])

  return <div>Hi</div>
}

When this component gets hot reloaded, a log line is printed.

variable changed "hello" "hello"

If this is an effect from the component getting remounted, I'd expect the ref to be cleared, oldValueRef.current to be undefined at the time the log is printed, and we would see

variable changed undefined "hello"

If it is just getting rerendered, the useEffect shouldn't be triggered from an unchanged string dependency, and there should be no logs at all.

What could cause this?

Package versions in use:

  • @pmmmwh/react-refresh-webpack-plugin 0.5.11
  • react-refresh 0.14.0
  • react 18.2.0 NOT using StrictMode (this definitely looks like StrictMode behaviour, but isn't)
  • webpack 4.46.0
@foucdeg foucdeg changed the title useEffect inside refreshed code runs needlessly useEffect inside refreshed code runs needlessly Mar 13, 2024
@pmmmwh
Copy link
Owner

pmmmwh commented Apr 27, 2024

This is expected behaviour from react-refresh. Effects are always re-run when components are hot-reloaded.

@pmmmwh pmmmwh closed this as completed Apr 27, 2024
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

2 participants