Paciolan / remote-component

Dynamically load a React Component from a URL
MIT License
591 stars 49 forks source link

How to set module's import name? #51

Closed blackraindrop closed 1 year ago

blackraindrop commented 2 years ago

Expected interface definitions

export interface UseRemoteComponentHook {
  (url: string, imports: string): [boolean, Error, (...unknown) => JSX.Element];
}
joelnet commented 2 years ago

I am not sure I fully understand the question. Could you elaborate a little bit?

mkikets99 commented 2 years ago

@blackraindrop Is it an issue? Can you please provide some logs and code fragments for analizing whats wrong?

blackraindrop commented 2 years ago

because remote module export some components

I can use imports to set named module with FetchRemoteComponent

export const fetchRemoteComponent: FetchRemoteComponent = ({
  requires,
  url,
  imports = "default"
}) => {
  const loadRemoteModule = createLoadRemoteModule({ requires });

  return loadRemoteModule(url).then(module => {
    const Component = module && module[imports];
    if (!Component) {
      throw new Error(`Could not load '${imports}' from '${url}'.`);
    }
    return Component;
  });
  // suppressHydrationWarning={true}
};

but useRemoteComponent can't use imports to set named module

const [loading, err, Component] = useRemoteComponent(url);
joelnet commented 1 year ago

@blackraindrop I just merged in this update. Test it and let me know if this is what you needed. Cheers 🍻