Skip to content

hooks inside deeply nested objects always cause state reset #1006

@adamschoenemann

Description

@adamschoenemann

Related plugins

Describe the bug

If using a hook that is referenced using two times dot-notation, the state will always reset upon saving a file; e.g:

const useTrue = () => {
  return true
}
const obj1 = {
  useTrue,
  obj2: { useTrue },
}

function App() {
  const [count, setCount] = useState(0)
  
  // will preserve the state
  const t1 = useTrue();
  // will also preserve the state
  const t2 = obj1.useTrue();
  // this will cause the state to always reset
  const t3 = obj1.obj2.useTrue();

  useEffect(() => {
    const interval = setInterval(() => setCount(c => c + 1), 1000);
    return () => { clearInterval(interval); }
  }, []);
  return <>{count}</>
}

It doesn't appear to be an issue in react-plugin-swc

Reproduction

https://github.com/adamschoenemann/vite-hmr-bug

Steps to reproduce

  • Use any hook defined inside two levels of nested objects.
  • Save the file and save it again: the second save should not reset the state, but it does

System Info

I couldn't get `envinfo` to install (got a 500 from npm, there's a cloudflare outage I think). But here is an `npm ls` from the repro repo:


├── @eslint/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @vitejs/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug: upstreamBug in a dependency of Vite

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions