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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New implementation #132

Open
alessandro308 opened this issue Mar 3, 2022 · 1 comment
Open

New implementation #132

alessandro308 opened this issue Mar 3, 2022 · 1 comment

Comments

@alessandro308
Copy link

alessandro308 commented Mar 3, 2022

I tried it but there was several errors with the latest versions of React.
At the end, I achieved basically the same result using https://github.com/httptoolkit/react-reverse-portal and building some component around it.

Basically I just created a Provider that store a list of mounted components (the core function is):

mountComponent(
        key, children,
) {
        const {components} = this.state;
        if(!components[key]){
            const node = createHtmlPortalNode();
            this.setState(({ components }) => ({
                components: {
                    ...components,
                    [key]: [node, children],
                },
            }));
            return node;
        } else {
            let node = components[key][0];
            this.setState(({ components }) => ({
                components: {
                    ...components,
                    [key]: [node, children],
                },
            }));
            return node;
        }
  }
 

and the render is like

<KeepALiveContext.Provider value={this.value}>
                {
                    Object.entries(components)
                    .map(([key, [node, component]]) =>
                        <InPortal node={node} key={key}>
                            {component}
                        </InPortal>
                    )
                }
                { children }
</KeepALiveContext.Provider>

and then I invoke this function in the KeepAlive component using the useEffect.

Since this implementation uses react-reverse-portal I can use the standard useEffect hooks my component and all looks like super.

What do you think to consider to rewrite react-keep-alive with this suggestion so to be more compatible with latest version of React and then remove all the event communication inside of it?

@shenjunru
Copy link

Give a try react-fiber-keep-alive, the react fiber based keep-alive implementation.

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