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

HMR with reacts appends a new app instance instead of replace the current #2509

Closed
brielov opened this issue Jan 4, 2019 · 7 comments · Fixed by #2676
Closed

HMR with reacts appends a new app instance instead of replace the current #2509

brielov opened this issue Jan 4, 2019 · 7 comments · Fixed by #2676

Comments

@brielov
Copy link

brielov commented Jan 4, 2019

I have a parcel + react + css modules project. Whenever I save a file, the entire application gets re-rendered and appended to the #root container instead of replacing the whole thing.

This is what my index.js file looks like:

import React, { StrictMode } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import { ApolloProvider } from 'react-apollo';
import './index.css';
import App from './app';
import client from './apollo-client';

ReactDOM.createRoot(document.getElementById('root')).render(
  <StrictMode>
    <Router>
      <ApolloProvider client={client}>
        <App />
      </ApolloProvider>
    </Router>
  </StrictMode>,
);

This is what it looks like when I save the same file three times.

screen shot 2019-01-04 at 12 10 09

What am I doing wrong?

@mischnic
Copy link
Member

mischnic commented Jan 4, 2019

Parcel's HMR simply reruns the code, it seems like ReactDOM.createRoot(document.getElementById('root')).render doesn't like that.

@brielov
Copy link
Author

brielov commented Jan 4, 2019

@mischnic Ok, thanks for the quick answer. So I've managed to solve it like this:

const rootNode = document.getElementById('root');

while (rootNode.lastChild) {
  rootNode.removeChild(rootNode.lastChild);
}

ReactDOM.createRoot(rootNode).render(
  <StrictMode>
    <Router>
      <ApolloProvider client={client}>
        <App />
      </ApolloProvider>
    </Router>
  </StrictMode>,
);

@pronevich
Copy link

@DeMoorJasper possibly this issue should be reopened. Parcel must support new react async mode (ReactDOM.createRoot)

@karansapolia
Copy link

karansapolia commented Feb 13, 2019

I would like to take this up. First issue here, would really appreciate if anyone could give me some pointers and direction.

@karansapolia
Copy link

karansapolia commented Feb 17, 2019

Pinging again, if any participant would like to guide through. I wish to get started on this at the earliest 😃

@mischnic
Copy link
Member

@karansapolia I wouldn't really consider this a bug in parcel itself, it's rather a side effect of how the hot module reloading works (and I don't really see how this can be fixed without being specific to react - and createRoot isnt even documented in the react api?)

I would recommend taking a look at a different issue.

@karansapolia
Copy link

@mischnic Thank you for the quick reply. If that's the case, sure I'll move to other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@pronevich @DeMoorJasper @mischnic @karansapolia @brielov and others