Last active
December 18, 2025 19:27
-
-
Save PierBover/1eb1dc122ce20859fd6c6a2e1c47f598 to your computer and use it in GitHub Desktop.
Keep alive Svelte action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const instances = {}; | |
| export default function (node, {id, componentClass}) { | |
| if (instances[id]) { | |
| node.appendChild(instances[id]); | |
| } else { | |
| const wrapper = document.createElement('div'); | |
| const instance = new componentClass({ | |
| target: wrapper | |
| }); | |
| instances[id] = wrapper; | |
| node.appendChild(wrapper); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script> | |
| import keepAlive from "./action.js"; | |
| import Component from './Component.svelte'; | |
| </script> | |
| <div use:keepAlive={{id: 'some-manual-id', componentClass: Component}}/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A sample solution for Svelte 5.