vitejs / vitejs/vite-plugin-react

hooks inside deeply nested objects always cause state reset

Open
#1,006 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug: upstream plugin: react
Dominant language
TypeScript
Stars
1.2k
Forks
269
Avg merge
1d 3h
Merged PRs (30d)
19

Description

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/js@9.39.1
├── @types/node@24.10.1
├── @types/react-dom@19.2.3
├── @types/react@19.2.7
├── @vitejs/plugin-react@5.1.1
├── babel-plugin-react-compiler@1.0.0
├── eslint-plugin-react-hooks@7.0.1
├── eslint-plugin-react-refresh@0.4.24
├── eslint@9.39.1
├── globals@16.5.0
├── react-dom@19.2.1
├── react@19.2.1
├── typescript-eslint@8.48.1
├── typescript@5.9.3
└── vite@7.2.6
Used Package Manager

npm

Logs

No response

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction and verify that the second save resets state when the hook is referenced through two nested objects. Compare the plugin-react behavior with plugin-react-swc, then trace the plugin-react package entry point handling this hook pattern. Done means nested hook references preserve state across repeated saves and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.