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

Changes in files injected with useContext() force vite-react project for full reload #2624

Closed
Relaxe111 opened this issue Mar 21, 2021 · 7 comments

Comments

@Relaxe111
Copy link

Relaxe111 commented Mar 21, 2021

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

When I change something in a .ts file it force to full reload the application, with losing the state of the application.
This is NOT related to mobx-state-tree, this happens even if i add a simple const test = 'test' field in any part of the .ts file.
I tried to migrate a big project to vite and it take long time to reload for each change in any .ts file, also after a while it hangs and wait for server to respond and it never updates. I think this is related to this issue.

vite react force full reload on changes in ts file

repo with the issue example: https://github.com/Relaxe111/vite-react-mst

  • vite version: 2.1.2
  • Operating System: ubuntu 18.04
  • Node version: 14
  • Package manager (npm/yarn/ppm) and version: yarn 1.22.10
@Relaxe111
Copy link
Author

I found out that issue is some how related to useContext(). When i import store directly, it works properly. But when i inject it with react context it does a full reload.

@Relaxe111 Relaxe111 changed the title Changes in .ts files force vite-react project for full reload Changes in files injected with useContext() force vite-react project for full reload Jun 20, 2021
@Relaxe111
Copy link
Author

I've seen similar opened issues not sure if they are related or not:
#3301
#2308

@iamhmx
Copy link

iamhmx commented Feb 22, 2022

@Relaxe111 Is there a solution to this problem?

@iamhmx
Copy link

iamhmx commented Feb 22, 2022

@patak-dev Is there a solution to this problem?

@adnanalbeda
Copy link

adnanalbeda commented May 25, 2022

I had the same issue. I was able to somehow solve it.

When HMR is called, I find this error in the console with the following message:

You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot()

After some search, here is the issue on SO.
I copied the implementation from the answer and paste it in my project, so my main.tsx looks like this now:

let container: HTMLElement | null = null;

document.addEventListener("DOMContentLoaded", function () {
  if (!container) {
    container = document.getElementById("root");
    if (!container) {
      console.log("No root element is found.");
    } else {
      const root = createRoot(container);
      root.render(
        <StrictMode>
          <StoreContextProvider>
            <CustomRouter history={history}>
              <App />
            </CustomRouter>
          </StoreContextProvider>
        </StrictMode>
      );
    }
  }
});

After I made this change, I can say that my app loading is way faster than the default implementation.
Also. HMR doesn't break now, but the context will lose it's data, unless you store them in localstorage.

@liho00
Copy link

liho00 commented Jul 3, 2022

React context is a great feature, however few issues related to react context from 2020, 2021 did not get resolved, i dont think vite is a good develeopment tools for react project, unless you dont using react context...

@sapphi-red
Copy link
Member

Closing as I confirmed this works after upgrading plugin-react-refresh to plugin-react.

related: #3301

@github-actions github-actions bot locked and limited conversation to collaborators Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants