Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Suspense lazy loading without fallback

I want to lazy load my components to decrease my initial bundle size and get components on the fly using code splitting using react router.

However, when using React Suspense, they force you to use a fallback for loading.
This wouldn't work:

const lazyLoadComponent = Component =>
    props => (
        <Suspense> // Missing fallback property
            <Component {...props} />
        </Suspense>
    );

In my case I am rendering html from the server so I don't want to use a spinner.
This would create a useless flicker on my screen! I.e.:

  • Html loads
  • Place holder appears
  • PageComponent for the route gets loaded
  • I have my own spinner that loads a feed from within the page component

In my case the html corresponds to the react component that gets loaded.

Is there any known hack to easily work around this problem (except for creating a loader for any route that copies the html (!!), which by the way, would make lazy loading useless).

I am a bit displeased with "forcing" us to add a loader and I don't understand the logic behind the decision to make it mandatory.

like image 800
html_programmer Avatar asked Aug 27 '20 22:08

html_programmer


People also ask

Can we use suspense without lazy loading?

Whilst we can render elements as children of our Suspense component the only benefit comes from using lazy components so that we can delay the loading of these components until we choose to do so. And that in turn leads us to code splitting our application to keep it nice and light / quick for the end user.

Is React suspense production ready?

Suspense is not a data fetching library. It's a mechanism for data fetching libraries to communicate to React that the data a component is reading is not ready yet. React can then wait for it to be ready and update the UI. At Facebook, we use Relay and its new Suspense integration.

What is lazy loading suspense How do they work?

In essence, lazy loading means that a component or a part of code must get loaded when it is required. It is also referred to as code splitting and data fetching . Talking about React specifically, it bundles the complete code and deploys all of it at the same time.

What is react lazy and react suspense with examples?

What is React Lazy and React suspense with examples 1 Network requests Without suspense. 2 With suspense. In suspense, we need to specify the fallback property so that suspense can load the fallback property at the time of that component is downloading. 3 Code splitting in React router by using suspense. ...

How to lazy load components in react lazy?

React suspense and React.lazy helps to lazy load the components so that users can only download the required data related to that page. Let’s see how it works. Network requests Without suspense. In suspense, we need to specify the fallback property so that suspense can load the fallback property at the time of that component is downloading.

How to fallback a React component while it is suspended?

Error: A react component suspended while rendering, but no fallback UI was specified. Add a Suspense fallback= component higher in the tree to provide a loading indicator or placeholder to display. The Suspense component allows us to fallback some content like loading indicator while we are waiting for loading the component.

Can I use a spinner with react suspense?

However, when using React Suspense, they force you to use a fallback for loading. In my case I am rendering html from the server so I don't want to use a spinner. This would create a useless flicker on my screen! I.e.: In my case the html corresponds to the react component that gets loaded.


Video Answer


2 Answers

I created an issue for this on Github: https://github.com/facebook/react/issues/19715

There isn't a current clean solution using React-Router / React.
This is however foreseen in a future release using concurrent mode. As mentioned by Dan Abramov:

Regarding your concrete feature request, I think I can reframe it slightly differently. It's not that you want "optional fallback" since that wouldn't make sense for new screens (we've got to show something). What I believe you're looking for is a way to skip showing the fallback if the content is already in HTML. This is precisely how React behaves in Concurrent Mode so the feature request is already implemented (and will eventually become the default behavior in a stable release).

For me it is not a problem to wait, so currently I will omit lazy-loading the routes as this concerns a hobby-project and I have time to wait for a future release.

like image 150
html_programmer Avatar answered Oct 06 '22 01:10

html_programmer


Try to use code splitting in the docs

fallback props is just a React element, you can set it for null.

const MyLazyComponent= React.lazy(() => import('./MyComponent'));

<Suspense fallback={null}>
    <MyLazyComponent />
</Suspense>
like image 41
tolotra Avatar answered Oct 05 '22 23:10

tolotra