Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to lazyload a hook? #975

Open
ion-willo opened this issue Jul 20, 2023 · 3 comments
Open

How to lazyload a hook? #975

ion-willo opened this issue Jul 20, 2023 · 3 comments

Comments

@ion-willo
Copy link

馃挰 Questions and Help

I have a module that exports both a React Component and a hook to configure the component.

I tried:

const Carousel  = loadable(() => import('../components/ui/Carousel'), {
  resolveComponent: (components) => components.Carousel
})
const useCarousel  = loadable.lib(() => import('../components/ui/Carousel'), {
  resolveComponent: (components) => components.useCarousel
})

But this failed the first time I tried calling useCarousel saying that useCarousel is not a function.

Any help would be appreciated.

@theKashey
Copy link
Collaborator

Look like a little gap in documentation. The result of any operation is a component.
loadable.lib is render prop pattern giving you access to any function "dynamically". Stil you cannot use it as you might want.

const Carousel  = loadable.lib(() => import('../components/ui/Carousel'));
//...
<Carousel>{({useCarousel}) => useCarousel()....}</Carousel>

If only loadable.lib had a hook form 馃槶

@ion-willo
Copy link
Author

Damn, that's a pity...

Is there a feature request I can vote on for implementing this?

@theKashey
Copy link
Collaborator

This library is not really under active development and frankly speaking even not under very active maintenance. So unless someone steps in to implement a few feature it will be not implemented.

At the same time this feature is "supported" by react-imported-component
"supported" because you our will get fancy API and waterfall requests, or not fancy code you might have to "micromanage". There is no preexisting example for "hooks", let me provide one here

const MyComponent = () => {
  // you can import whatever you want
  const {
      imported: useCarousel,
      loadable: carouselLoadable
    } = useImported(() => import('../components/ui/Carousel'),(components) => components.useCarousel );

   // but in order to "obey" the rule of hooks you need to "break" this component
   if(!useCarousel){
     throw carouselLoadable.resolution;
   }
   // you can combine `useImported` and `throw` in a custom hook, but that would cause waterfalls
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants