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-sm

v1.1.0

Published

No non-sense SolidJS global state management

Downloads

1

Readme

Solid SM

No non-sense global state management for SolidJS, inspired by Zustand.

import { state } from "solid-sm"

type TaskState = {
    name: string
    done: boolean
    complete(): void
}

type RootState = {
    tasks: TaskState[]
    addTask(name: string): void
}

const createTask = (name: string) => {
    return state<TaskState>((set) => ({
        name,
        done: false,
        complete() {
            set("done", true)
        },
    }))
}

const rootState = state<RootState>((set) => ({
    tasks: [],
    addTask(name) {
        set("tasks", (t) => [...t, createTask(name)])
    },
}))

Features

  • Only one function, for creating a state. That's it.
  • Compatible with SolidJS functions and design patterns.
  • Nestable. Create and update nested states easily.

Motivation

Although Solid provides global state out of the box, dealing with nested states is complex and requires a lot of boilerplate. This module aims to simplify that, reducing the necessary effort to create idiomatic and performant designs.

Usage

Creating a new state

A state is a reactive object with data and actions that can mutate this data. Having actions mixed with data is important because it allows consumers to handle the data without having to known how the data was created. To create a new state, use the state function. It takes setup a callback that returns the initial value.

type CounterState = {
    value: number
}

const counter = state<Counter>(() => ({
    value: 0,
}))

Consuming a state inside a component

Inside components, states behaves as any other reactive object. You can access its properties inside a reactive scope to subscribe it to changes in the property.

function Counter() {
    return <div>Counter: {counter.value}</div>
}

Creating actions to update a state

The state setup callback takes as parameter a set function that can update the state value after it's initialized. With it, you can create actions that will allow the state to be updated by consumers.

type CounterState = {
    value: number
    inc(): void
}

const counter = state<Counter>((set) => ({
    value: 0,
    inc(): {
        // The passed object will be shallowly merged with the current value
        set((s) => ({ value: s.value + 1 }))
        // You can also pass the property that will be updated instead
        set("value", (v) => v + 1)
    }
}))

Using actions in reactive scopes

Solid SM provides a helper function to unwarp actions from the state object. This is useful when using the action directly as a event handler.

type CounterState = {
    value: number
    inc(): void
}

const counter = state<Counter>((set) => ({
    value: 0,
    inc(): {
        set("value", (v) => v + 1)
    }
}))

function Counter() {
    const [inc] = useActions(counter, "inc")

    return (
        <div>
            {counter.value}{" "}
            <button onClick={inc}>Increment</button>
        </div>
    )
}

Creating nested states

Nested states are a way of updating part of a object inside a state without updating the parent state. This simplifies the handling of complex data involving array of objects.

type BookState = {
    title: string
    score: number | null
    setScore(score: number): void
}

type AuthorState = {
    books: BookState[]
    addBook(title: string): void
}

const author = state<AuthorState>((set) => ({
    books: [],
    addBook(title) {
        set("books", (b) => [
            ...b,
            state<BookState>((setBook) => ({
                title,
                score: null,
                setScore(score) {
                    setBook("score", score)
                },
            })),
        ])
    },
}))