question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HMR breaks when modifying React context provider

See original GitHub issue

Describe the bug

Vite HMR breaks when modifying React context provider Related: https://github.com/vitejs/vite-plugin-react/issues/24

Reproduction

selrond/vite-react-usecontext

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 413.26 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.7.6 - ~/.nvm/versions/node/v15.14.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 89.1.21.76
    Chrome: 90.0.4430.93
    Firefox: 87.0
    Firefox Developer Edition: 89.0
    Safari: 14.0.3
  npmPackages:
    vite: ^2.2.3 => 2.2.4 

Used package manager: npm

Logs


Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:57
  • Comments:51 (6 by maintainers)

github_iconTop GitHub Comments

19reactions
pablocdevcommented, Mar 27, 2022

Same issue here, but im trying to be more specific with that.

In my experience:

  1. IN THE SAME FILE. Creates a context, and exports a provider
export const Context = createContext();

export const Provider = ({children}) => (
  <Context.Provider value={/* whatever*/}>{children}</Context.Provider>
)

Running npm run dev works. Forcing a hot reload on this file, crashes (context gets default value for all useContext calls).

  1. IN SEPARATED FILES
export const Context = createContext();
import { Context } from './my-context-file';

export const Provider = ({children}) => (
  <Context.Provider value={/* whatever*/}>{children}</Context.Provider>
)

Running npm run dev works. Forcing a hot reload on any of those files, still works.

Tell me if you need more info, or a minium reproduction repo.

Hope it helps!

16reactions
IanVScommented, Sep 26, 2022

I’ve pushed up a PR that fixes this issue, according to a test I wrote based off the reproduction in the issue description. But, it requires a change to how Vite handles import.meta.hot.invalidate(), so I think it will need some discussion with the Vite team.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Context HMR still has problem. Due to ... - Hacker News
React falters because there are not many community contributing towards react integration.
Read more >
Hot Reloading in React. or, an Ode to Accidental Complexity
Before React Hot Loader, my first thought was that I could use Webpack HMR to replace the root component of my app and...
Read more >
React Context State Update - Stack Overflow
You are not passing setUser to the StoreProvider value which means that you can't destructure it, and can't update your state. Later you...
Read more >
Next.js: The Good, Bad and Ugly - An Idiosyncratic Blog
Context : React context providers live here. ... This can be fixed by extending the css-loader config and changing mode: 'local' property.
Read more >
HMR API - Vite
This is the client HMR API. For handling HMR update in plugins, see handleHotUpdate. The manual HMR API is primarily intended for framework...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found