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-primitives/lifecycle

v0.0.102

Published

Package providing extra layer of lifecycle primitives for Solid.

Downloads

319

Readme

@solid-primitives/lifecycle

turborepo size version stage

Package providing extra layer of lifecycle primitives for Solid.

  • createIsMounted - Returns a boolean signal indicating whether the component is mounted or not.
  • isHydrated - A signal with the same behavior as isHydrating but this one focused only on client-side updates.
  • onElementConnect - Calls the given callback when the target element is connected to the DOM.

Installation

npm install @solid-primitives/lifecycle
# or
yarn add @solid-primitives/lifecycle
# or
pnpm add @solid-primitives/lifecycle

createIsMounted

Returns a boolean signal indicating whether the component is mounted or not.

It's a simple wrapper around createSignal and onMount, but it can make your code feel more declarative - especially when used with createMemo.

import { createIsMounted } from "@solid-primitives/lifecycle";

const isMounted = createIsMounted();
const windowWidth = createMemo(() => (isMounted() ? ref.offsetWidth : 0));

let ref: HTMLElement;
<div ref={ref}>{windowWidth()}</div>;

isHydrated

A signal accessor indicating if the owner is done hydrating.

  • false during SSR (always)
  • false on the client if the component evaluation is during a hydration process.
  • true on the client if the component evaluates after hydration or during clinet-side rendering.

If it returns true it means that you can safely change the initial values of signals that are used in the JSX, without causing a mismatch between the server and client.

It can be used in computations as a signal, and it will trigger a re-evaluation when the hydration state changes.

But it can also be used as a simple check of the hydration state.

import { isHydrated } from "@solid-primitives/lifecycle";

const serverFallback = 0;

const [vw, setVw] = createSignal(
  // if the component is hydrated, we can safely use the window width immediately
  isHydrated() ? window.innerWidth / 100 : serverFallback,
);

<p>Window width: {vw()}vw</p>;

Implementing ClientOnly component

isHydrated can be used to easily implement a ClientOnly component that will only render its children on the client.

import { createMemo, FlowComponent, JSX } from "solid-js";
import { isHydrated } from "@solid-primitives/lifecycle";

// This component will only render its children on the client
export const ClientOnly: FlowComponent = props => {
  const children = createMemo(() => isHydrated() && props.children);
  return <>{children()}</>;
};

// Usage
<ClientOnly>
  <ComponentThatBreaksOnServer />
</ClientOnly>;

onElementConnect

onMount is a common lifecycle hook that is used to perform side-effects when the component is mounted. However, it is not certain that the elements are actually connected to the DOM when the mount callback is called.

Note If that's the case, it might be a sign that you are executing components that are not visible to the users my mistake.

And if this is something intentional, you probably already have a way to hook into the actual DOM rendering.

If you are not sure, you can use onElementConnect instead of onMount to make sure that you are caling your callback when the elements are connected to the DOM.

<div
  ref={el => {
    // often false, but will be true during hydration
    el.isConnected;

    onMount(() => {
      // often true, but will be false if the executed component is not actually getting rendered
      el.isConnected;
    });

    onElementConnect(el, () => {
      // always true
      el.isConnected;
    });
  }}
/>

Demo

You can see the primitives in action in the following sandbox: https://primitives.solidjs.community/playground/lifecycle/

Changelog

See CHANGELOG.md