React.lazy remounts component if used within a render
See original GitHub issueDo 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:
- Created 5 years ago
- Comments:19 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Solution from @maulik-soni works for me if I wrap the output in
useMemo
Because you are missing the point @Haraldson.
React.lazy
is always returning a new component. Because it’s a factorylazy
has an internal state, which knows is itnot-loader
/loading
orloaded
. Once it “loaded” - it never refetches the code. NEVER.lazy
expects someimporter
- a function which will return a Promise withdefault
, 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.For example -
loadable-components
will use babel-plugin to extract file name fromimport
, 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 differentkey
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.