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

@cceevv/tinystore

v1.1.1

Published

Tiny state manager based on React Hooks, with automatic performance optimization.

Downloads

4

Readme

Tiny state manager based on React Hooks, with automatic performance optimization.

npm GitHub Workflow Status Coverage Status npm bundle size npm type definitions GitHub

English · 简体中文


Features

  • Amazing re-render auto-optimization
  • Extremely simple API
  • Less than 100 lines of source code

Install

pnpm add @cceevv/tinystore
# or
yarn add @cceevv/tinystore
# or
npm i @cceevv/tinystore

Usage

1. State definition

State is a simple class with no methods, mainly used to define data types and structures.

interface Point {
  x: number;
  y: number;
}

class DemoState {
  label = "";
  num = 0;
  point: Point = {
    x: 0,
    y: 0,
  };
}

2. Action definition

Action is a class that contains a series of methods for changing the State, and the state can only be changed in Action.

import type { Getter, Setter } from "@cceevv/tinystore";

class DemoAction {
  constructor(
    // Constructor Shorthand
    private get: Getter<DemoState>,
    private set: Setter<DemoState>,
  ) {
    set({ label: "Hello Kitty." });
  }

  inc() {
    const { num } = this.get();
    this.set({ num: num + 1 });
  }

  setPoint(x: number, y: number) {
    this.set({ point: { x, y } });
  }

  private readonly names = ["Aaron", "Petter", "Charles"];

  // async example
  async randomName() {
    await new Promise((resolve) => setTimeout(resolve, 10));
    this.set({ label: this.names[Math.random() * this.names.length | 0] });
  }
}

3. tinyStore initialization

It is recommended that steps 1~3 be placed in one file.

import tinyStore from "@cceevv/tinystore";

export default tinyStore(DemoState, DemoAction);

4. Access state and actions in components

import store from "path/to/store";

const Demo = () => {
  const { label, num, point } = store.useStore();
  const { inc, setPoint, randomName } = store.actions();

  return (
    <>
      <p>
        <label>num:</label>
        <span>{num}</span>
      </p>
      <button onClick={inc}>inc</button>

      <p>
        <label>point:</label>
        <span>[{point.x}, {point.y}]</span>
      </p>
      <button onClick={() => setPoint(111, 222)}>setPoint</button>

      <p>
        <label>label:</label>
        <span>{label}</span>
      </p>
      <button onClick={randomName}>randomName</button>
    </>
  );
};

API

tinyStore(StateClass, ActionClass)

  • StateClass: A simple class without methods, mainly used to define data types and structures.
  • ActionClass: A class that contains a set of methods for changing the State.
  • returns: {useStore, getStore, actions} See the explanation below for details.

`StateClass` and `ActionClass` will be automatically initialized, and `get` and `set` methods will be injected into the constructor of `ActionClass` to read and write `State`, `State` can only be modified in `Action` through `set()` method.

useStore()

This is a React Hook that returns all state, but only the state that is used in the component will trigger a React render.

Note: The return value is read-only and cannot be modified. If the parameter `true` is passed in, the source data will be returned, which can be modified, and it is generally not recommended!

getStore()

Returns all state. The difference from useStore() is that getStore() can be called anywhere, not just in React components.

Note: The return value is read-only and cannot be modified. If the parameter `true` is passed in, the source data will be returned, which can be modified, and it is generally not recommended!

actions()

Returns all methods used to change State, supports asynchronous methods.

The return values of `get()`, `useStore()`, `getStore()`, `actions()` are all read-only, which means it cannot be modified!

License

MIT License (c) cceevv