npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

solid-keep-alive

v0.2.3

Published

![npm](https://img.shields.io/npm/dw/solid-keep-alive?style=for-the-badge) ![GitHub Repo stars](https://img.shields.io/github/stars/JulianSoto/solid-keep-alive?style=for-the-badge)

Downloads

6

Readme

solid-keep-alive

npm GitHub Repo stars

Keep you components alive even after parent's unmounts, saving signals and DOM elements in cache to reuse them.

Demo

On StackBlitz: stackblitz.com/edit/solid-keep-alive-demo

Installation

With npm

npm install solid-keep-alive

With yarn

yarn add solid-keep-alive

Usage

Wrap components that you want to keep in cache in KeepAlive tags:

<KeepAlive id="unique-const-id">
  <Child1 />
  {/* ... */}
</KeepAlive>

But first, KeepAlive components need a context, so all of them MUST be a descendant of KeepAliveProvider. For this you have to wrap your app (preferably) in this provider.

import { render } from 'solid-js/web';
import App from './App';
import { KeepAliveProvider } from 'solid-keep-alive';

render(
  () => (
    <KeepAliveProvider>
      <App />
    </KeepAliveProvider>
  ),
  document.getElementById('root')
);

You can also put the provider anywhere in the tree, as long as all KeepAlive components are descendant of at least one. However you need to be cautious. Having multiple of these context providers in the same app is discouraged because of memory consumption.

Now you can use KeepAlive to cache parts of you application.

// ...
const [selected, setSelected] = createSignal('first');

return (
  <>
    <Switch>
      <Match when={selected() === 'first'}>
        <KeepAlive id="first-tab">
          {/* everything inside here is kept alive, even after Match unmounts */}
          <FirstView />
          <SomeOtherComponent />
        </KeepAlive>
      </Match>
      <Match when={selected() === 'second'}>
        <KeepAlive id="second-tab">
          {/* this too */}
          <SecondView />
        </KeepAlive>
      </Match>
      <Match when={selected() === 'third'}>
        {/* this is not kept alive, everything here is unmounted immediately */}
        <ThirdView />
      </Match>
    </Switch>
    {/* ... */}
  </>
);

In this example, when a Match unmounts, rather than unmounting children immediately, KeepAlive takes ownership of them and saves them in cache, so when Match is mounted again children don't need to be remounted, keeping the exact same signals and DOM elements.

Elements from cache are automatically remove when the maximum amount of elements is reached. Default is 10, but you can change this by passing a maxElements prop to the context provider:

<KeepAliveProvider maxElements={20}>
  <App />
</KeepAliveProvider>

To manually dispose elements from cache use the removeElement method from the useKeepAlive hook:

import { useKeepAlive } from 'solid-keep-alive';

const App = () => {
  const [elements, { removeElement }] = useKeepAlive();
  const clickHandler = () => {
    // manually remove cached element where id is 'home-view'
    removeElement('home-view');
  };
  // ...
};