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-hooks/state

v0.1.9

Published

global state management for solid-js

Downloads

3

Readme

@solid-hooks/state

Pinia like global state management for solid.js

Install

npm i @solid-hooks/state
yarn add @solid-hooks/state
pnpm add @solid-hooks/state

Usage

support run without provider (use createRoot)

import { defineState, GlobalStateProvider, persistStateFn, storageSync } from '@solid-hooks/state'

// like Pinia's Option Store
const useTestState = defineState('test', {
  init: { value: 1, deep: { data: 'hello' } },
  getter: state => ({
    // without param, will auto wrapped with `createMemo`
    doubleValue() {
      return state.value * 2
    },
  }),
  action: (setState, state, utils) => ({
    plus(num: number) {
      setState('value', value => value + num)
    },
  }),
})

// usage
const [state, actions] = useTestState()

render(() => (
  <GlobalStateProvider>
    state:
    {' '}
    <p>{state().value}</p>

    getter:
    {' '}
    <p>{state.doubleValue()}</p>
    getter:
    {' '}
    <p>{getters.doubleValue()}</p>

    action:
    {' '}
    <button onClick={actions.double}>double</button>
    <br />
    action:
    {' '}
    <button onClick={() => actions.plus(2)}>plus 2</button>
  </GlobalStateProvider>
))

// use produce
state.$patch((state) => {
  state.deep.data = 'patch'
})
// use reconcile but support partial state
state.$patch({
  test: 2
})

// createEffect(on()), defer by default
state.$subscribe(
  s => s.deep.data, // or state access path ('deep.data')
  (state, oldState) => console.log(state, oldState),
  { defer: false },
)

// reset
state.$reset()

or just a global-level context & provider

import { defineState } from '@solid-hooks/state'
import { createEffect, createMemo, createSignal } from 'solid-js'

export const useCustomState = defineState('custom', (name, log) => {
  const [plain, setPlain] = createSignal(1)
  createEffect(() => {
    log('defineState with custom function:', { name, newValue: plain() })
  })
  const plus2 = createMemo(() => plain() + 2)
  function add() {
    setPlain(p => p + 1)
  }
  return { plain, plus2, add }
})

Without provider

import { defineGlobalState } from '@solid-hooks/state'

const useTestState = defineGlobalState('test', {
  init: { value: 1, deep: { data: 'hello' } },
  getter: state => ({
    doubleValue() {
      return state.value * 2
    },
  }),
  action: (setState, state, utils) => ({
    plus(num: number) {
      setState('value', value => value + num)
    },
  }),
})

Persist

import { defineState } from '@solid-hooks/state'
import { persistStateFn, storageSync } from '@solid-hooks/state/persist'

const useTestState = defineState('test', {
  init,
  // ...
  // custom state function
  stateFn: persistStateFn({
    key: 'other-key', // state.$id by default
    serializer: { write: JSON.stringify, read: JSON.parse, }, // JSON by default
    storage: localStorage, // localStorage by default, async storage available
    path: ['test'], // type-safe state access path for persisted state, support array
    sync: storageSync, // sync persisted data
  }),
})

IndexedDB

import { createIdbStorage, persistStateFn } from '@solid-hooks/state/persist'

const idbStorage = createIdbStorage('db-name')
const stateFn = persistStateFn({
  storage: idbStorage,
  // ...
})

Utils

Functions used in defineState with object

/**
 * create state with utils, use in `SetupObject`
 */
function createStateWithUtils<T extends object>(
  stateName: string,
  initialState: T,
  stateFn?: StateFn<T>
): [state: T, setState: SetStoreFunction<T>, utils: StateUtils<T>]
/**
 * create getters, wrap non-param function with `createMemo`
 *
 * use in `SetupObject`
 */
function createStateGetter<T extends GetterObject>(getters?: T): T
/**
 * create actions, wrap functions with `batch(() => untrack(() => ...))`
 *
 * use in `SetupObject`
 */
function createStateAction<T extends ActionObject>(actions?: T): T

deepClone

globalThis.structuredClone, fallback to klona

Credit