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.

Creating a reusable component

See original GitHub issue

Is it possible to create a reusable component so that we don’t have to repeat ourselves like this all the time:

const One = Loadable({
  loader: () => import('./components/One'),
  loading() {
    return <Preloader />
  }
});
const Two = Loadable({
  loader: () => import('./components/Two'),
  loading() {
    return <Preloader />
  }
});

I tried to do it like this:

import Loadable from 'loadable-components';

const Load = ({ loader, ...otherProps }) => {
  const LoadableComponent = Loadable(loader, { LoadingComponent: Preloader });
  return <LoadableComponent {...otherProps} />;
};

and then:

<Route path="/one" render={props => <Load loader={() => import('components/One')} />} />
<Route path="/two" render={props => <Load loader={() => import('components/Two')} />} />

But I start getting into weird infinite loops with this approach. Any ideas?

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
gregbergecommented, Nov 23, 2018
// custom-loadable.js
import loadable from '@loadable/component'

export default fn => loadable(fn, { fallback: 'Loading...' })
// app.js
import loadable from './custom-loadable'

const OtherComponent = loadable(() => import('./OtherComponent'))

If you want to be compatible with SSR, using the loadable name is required.

3reactions
sagar-gavhanecommented, Nov 23, 2018

react-loadable library given demonstrated on HOC component to avoid repetitions inside FAQ’s

https://github.com/jamiebuilds/react-loadable#how-do-i-avoid-repetition

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Create a Truly Reusable React Component from Scratch
How to Create a Reusable React Component ... Now, create an InputControl.js file inside the components folder with the following code: ... current...
Read more >
A Practical Guide to Creating Reusable React Components
Top Three Indicators of a Reusable React Component · Repetitive creation of wrappers with the same CSS style · Repetitive use of event...
Read more >
How to Create a Reusable React Form component - Section.io
A Reusable component is a piece of User Interface that can be used in many parts of an application to build and render...
Read more >
Reusable Components in React — A Practical Guide
So, to create a re-usable component in React, all you need to do is create a function that has a capital letter as...
Read more >
How to make your React components more reusable
How to make your React components more reusable · Use container component pattern · Generic versus specialized components · Be mindful about what ......
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