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.

Support for "High Order Component" / Wrapper / Forwarding

See original GitHub issue

Is your feature request related to a problem? Please describe.

I want to build a “transparent” lazy-loading component. This component should load a target component asynchronously, and when ready, delegate everything to this target.

This lazy-component should be generic: it should work for every target component. Moreover, I don’t want the consumer of the component to know there is this wrapper, it should be “transparent” for it.

Today, it doesn’t seem to be possible to build such “wrapper” because :

  • There is no API to forwards slots to the delegate ;
  • There is no API to forwards events to the delegate ;
  • There is no API to forwards “bind” from the parent to the delegate ;

There is various issues about this : #2837 about forwardings events ; #1824 and #4295 about forwardings slots ; I found nothing about “binding” forwardings. #4647 is about a loadable component, closed because “svelte-loadable” can implements this issue. however, it’s false because it doesn’t supports slots or events.

Describe the solution you’d like

An official way/api to “wrapper” transparently another component.

Describe alternatives you’ve considered

I am able to prototype such wrapper with internals API, however I am not able to forwards binding from parent to child :

<svelte:component this={cpn} {...slotsProps} {...$$restProps} bind:this={instance}/>

<script>
    import { get_current_component } from 'svelte/internal';
    
    export let provider;
    
    const slotsProps = {"$$slots":$$props.$$slots, "$$scope":$$props.$$scope};
    const self = get_current_component();
    
    let cpn;
    let instance;
        
    provider().then(result => cpn = result);
    
    $: if (instance) {
        for (let [type, listeners] of Object.entries(self.$$.callbacks)) {
            instance.$on(type, (e) => {
                listeners.forEach(l => l(e));
            });
        }
    }
</script>

How important is this feature to you?

I can’t today implements this kind of component. I’d like to “wrap” heavyweight components without updating or adding loading logic to every consumer.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Sxxovcommented, Aug 1, 2021

I’ve written an RFC at https://github.com/sveltejs/rfcs/pull/57 in an attempt to propose a solution for this. Anyone one looking at this problem should feel free to chime in.

0reactions
stale[bot]commented, Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Higher-Order Components - React
A higher -order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API,...
Read more >
React Router: Redirect with Higher-Order Component
Therefore, you can use a higher-order component (HOC) for the redirect, because when wrapping the component into a HOC, the logic of the...
Read more >
How to Use React Higher-Order Components | by Ross Bulat
Higher-Order Components (or HOCs) in React are functions that take a component and return a new component, enhancing the original in some way:...
Read more >
Implement a Higher Order Component | egghead.io
[04:27] In our case, our wrapper component accepts props and a ref from react forward ref, and then renders our toggle consumer which...
Read more >
Using Higher Order Components for Authenticated Routing
A Higher Order Component (HOC) (https://reactjs.org/docs/higher-order-components.html) is just a React Component that wraps another one.
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