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.

React.lazy remounts component if used within a render

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Simple reproduction here: https://codesandbox.io/s/6z4qmm2k7n

The contrived example does not make much sense, but I got burned by this in the context of react-router. I simply wanted to avoid defining all lazily loaded components up front but instead have them coupled with a particular route.

function Routing() {
  <>
    <Route path="foo" component={lazy(() => import('./foo'))} />
  </>
}

What is the expected behavior?

In case this is intended behavior, it should be definitely documented. Otherwise it would be nice to have some memoized lazy variant. Possibly with hooks coming what about useLazy ? 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
hypeJunctioncommented, Nov 19, 2020

Solution from @maulik-soni works for me if I wrap the output in useMemo

const CodesIndex = useMemo(() => LazyloadComponent(() => import('./Screens/Codes'))(true), []);

return (
   <Route path="/codes" components={CodesIndex} />
);
5reactions
theKasheycommented, Nov 18, 2019

Because you are missing the point @Haraldson.

  • React.lazy is always returning a new component. Because it’s a factory
  • every lazy has an internal state, which knows is it not-loader/loading or loaded. Once it “loaded” - it never refetches the code. NEVER.
  • lazy expects some importer - a function which will return a Promise with default, and it could be anything. It could not, and should “think” what it is, and every time you will give it a “new” importer. The same problem as with “new” lazy itself.
  • However, SSR requires to “knew” which bundles has to be loaded to fulfil requirements for “lazy components” on the current page. This is “the track” you are looking for.

For example - loadable-components will use babel-plugin to extract file name from import, and will synchronously import required component, as long as they “knew” - their code is loaded. However, as long as any factory will generate a unique result every time - you will experience a full tree remount. Like using a different key on the node.

So, please, just try to understand how it works, why it works that way, and why it’s not working for your case, and what’s is wrong with your case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - React Lazy loaded component loosing it's state (gets ...
To solve this issue use React.useMemo() inside DynamicLoader to memoize the current LazyComponent , and only recreates it if props.component ...
Read more >
Code-Splitting - React
The React.lazy function lets you render a dynamic import as a regular component. Before: import OtherComponent from '.
Read more >
Lazy loading React components - LogRocket Blog
A component created using React.lazy() is loaded only when it needs to be rendered. Therefore, you should display some kind of placeholder ...
Read more >
The Subtle Aspects of React Re-rendering (Part 1) - Medium
The fact that React delays rendering until knowing whether it is needed is called “lazy” evaluation. React optimizations. Those two examples ...
Read more >
Use React.lazy() to render components conditionally
You can use the React.lazy() function conditionally to ensure a component is rendered only if it is needed. OSF makes extensive use of...
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