React: change the component tree without unmounting/remounting certain components?

I have a wrapper component that’s dynamically loaded. While it’s loading, it’s just a div. I dynamically loaded it because it uses large libraries that I don’t want to include with the initial load.

E.g. the component tree looks like:

<WrapperDeferred>
  <Child />
</WrapperDeferred>

WrapperDeferred looks something like:

let Wrapper;

function WrapperDeferred({ children }) {
  const forceUpdate = useForceUpdate();

  useEffect(() => {
    if (!Wrapper) {
      import('./Wrapper').then(module => {
        Wrapper = module.default;
        forceUpdate();
      });
    }
  }, []);

  return Wrapper ? <Wrapper>{children}</Wrapper> : <div>{children}</div>;
}

The issue is that while Wrapper is loading, the child component renders and mounts. Then, when Wrapper loads, the child component unmounts, rerenders, and remounts. Is there a way to reuse the existing child component?

120 thoughts on “React: change the component tree without unmounting/remounting certain components?”

  1. 389300 771273Wow, great weblog layout! How lengthy have you ever been running a weblog for? you make running a blog glance effortless. The total appear of your internet site is magnificent, effectively the content material material! 935689

    Reply
  2. Very nice post. I just stumbled upon your blog and wished to
    say that I have truly enjoyed surfing around your blog posts.
    In any case I’ll be subscribing to your feed and I
    hope you write again soon!

    Reply
  3. Hi, Neat post. There’s an issue with your site in web explorer, could check this?
    IE nonetheless is the market leader and a huge element of other folks will omit your wonderful
    writing because of this problem.

    Reply
  4. Appreciating the hard work you put into your site and in depth information you present.
    It’s good to come across a blog every once in a while that isn’t the
    same outdated rehashed information. Wonderful
    read! I’ve saved your site and I’m including your RSS feeds to my Google account.

    Reply
  5. hello!,I like your writing very much! share
    we be in contact extra approximately your article on AOL? I
    need an expert on this space to solve my problem. May be that’s you!
    Taking a look forward to look you.

    Reply
  6. Thank you a lot for sharing this with all folks you actually recognize what you’re speaking about!

    Bookmarked. Kindly additionally discuss with my website
    =). We can have a link change agreement between us

    Reply
  7. Awesome issues here. I’m very glad to see your post.
    Thank you so much and I’m having a look forward to touch you.

    Will you kindly drop me a mail?

    Reply
  8. An interesting discussion is definitely worth comment.

    I believe that you ought to publish more about this subject
    matter, it may not be a taboo matter but typically folks don’t talk about such topics.
    To the next! All the best!!

    Reply
  9. Everything is very open with a clear description of the challenges.
    It was really informative. Your website is extremely helpful.
    Thank you for sharing!

    Reply
  10. It’s hard to come by well-informed people in this
    particular subject, however, you seem like you
    know what you’re talking about! Thanks

    Reply
  11. Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using
    for this website? I’m getting fed up of WordPress because I’ve had problems with hackers
    and I’m looking at options for another platform.
    I would be awesome if you could point me in the direction of a good platform.

    Reply
  12. Hello! Someone in my Myspace group shared this website with us so
    I came to give it a look. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!
    Superb blog and wonderful design.

    Reply
  13. Nice blog here! Also your web site loads up fast! What web host are you using?
    Can I get your affiliate link to your host? I wish my site loaded up as quickly
    as yours lol

    Reply
  14. I will right away take hold of your rss feed as
    I can not in finding your e-mail subscription hyperlink
    or newsletter service. Do you’ve any? Kindly permit me recognize in order that
    I may subscribe. Thanks.

    Reply
  15. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to
    my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some
    time and was hoping maybe you would have some experience with something like
    this. Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

    Reply
  16. Your style is very unique in comparison to other people I’ve read stuff from.
    Thank you for posting when you’ve got the opportunity,
    Guess I will just bookmark this web site.

    Reply
  17. I do consider all of the ideas you’ve offered in your post.
    They are really convincing and will certainly work.

    Still, the posts are very quick for starters.
    Could you please extend them a bit from next time? Thank
    you for the post.

    Reply
  18. Thank you for some other great post. The place else could anybody
    get that kind of information in such a perfect manner of writing?

    I have a presentation subsequent week, and I am
    on the search for such information.

    Reply
  19. I was suggested this blog by my cousin. I’m not sure whether this
    post is written by him as nobody else know such detailed about my problem.
    You’re amazing! Thanks!

    Reply
  20. Fantastic website. Plenty of useful information here.
    I am sending it to several buddies ans also sharing in delicious.
    And certainly, thanks in your effort!

    Reply
  21. Simply desire to say your article is as astonishing.
    The clearness in your post is simply excellent and i can assume you
    are an expert on this subject. Fine with your permission allow me to grab your feed to keep updated
    with forthcoming post. Thanks a million and please keep up the gratifying work.

    Reply
  22. Hmm is anyone else experiencing problems with the pictures on this blog
    loading? I’m trying to find out if its a problem on my end
    or if it’s the blog. Any suggestions would be greatly appreciated.

    Reply
  23. Hey this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding skills so I
    wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

    Reply
  24. Hi, I think your website might be having browser compatibility issues.
    When I look at your blog site in Firefox, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give you a quick heads up!
    Other then that, awesome blog!

    Reply
  25. Thanks for finally talking about > React: change the
    component tree without unmounting/remounting certain components?
    – JavaScript Help < Liked it!

    Reply
  26. My spouse and I absolutely love your blog and find nearly all of your post’s to be what precisely I’m
    looking for. Do you offer guest writers to write content
    available for you? I wouldn’t mind producing a post or
    elaborating on a number of the subjects you write
    concerning here. Again, awesome website!

    Reply
  27. It is the best time to make a few plans for the
    longer term and it is time to be happy. I’ve learn this publish and if I may just I desire to
    recommend you some interesting things or advice. Maybe you
    can write next articles regarding this article. I want to
    read more issues about it!

    Reply
  28. Great blog! Do you have any tips and hints for aspiring writers?
    I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed ..
    Any recommendations? Kudos!

    Reply
  29. Hey there I am so thrilled I found your web site, I really found you
    by error, while I was browsing on Google for
    something else, Nonetheless I am here now and would just
    like to say many thanks for a fantastic post and
    a all round entertaining blog (I also love the theme/design), I don’t have time to look over it
    all at the moment but I have saved it and also included
    your RSS feeds, so when I have time I will be back to
    read much more, Please do keep up the awesome jo.

    Reply
  30. An outstanding share! I have just forwarded this onto a colleague who had been conducting a little research on this.
    And he actually ordered me breakfast due
    to the fact that I found it for him… lol. So allow me to reword this….
    Thanks for the meal!! But yeah, thanx for spending the time
    to discuss this subject here on your web site.

    Reply
  31. Hello there, I found your site by means of Google whilst searching for a related
    subject, your website came up, it seems to be good. I have bookmarked it in my google bookmarks.

    Hi there, simply become aware of your weblog via Google,
    and found that it’s really informative. I am gonna watch out for brussels.
    I’ll appreciate in the event you proceed this in future.
    A lot of other people might be benefited out of your writing.

    Cheers!

    Reply
  32. Good day! I know this is kinda off topic but I was wondering which blog platform are you
    uding for this website? I’m getting sick aand tireed of
    Wordpress because I’vehad issues with hackers and I’m looking
    at oprions for another platform. I would be
    fntastic if you could point me in the irection of a goid platform.

    Here is my website: Chaturbate

    Reply