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

@ivbrajkovic/simple-state

v1.5.1

Published

Simple reactive state

Downloads

1

Readme

:zap: Simple reactive state

Ultra fast, ultra simple, zero configuration and zero dependency

Simple state management that can be used for sharing reactive properties among component on deferent position and level in tree branch without need for context or providers. Just import a hook and use it in every component you need and only this component will rerender on change.

Hook usage is similar to well known useState hook :wink:


🚀 Demo - Codesandbox basic example

Basic usage

Store module

// store.js
import { makeObservable, useSimpleState } from "@ivbrajkovic/simple-state";

// This can be an object with many properties
const store = makeObservable({ name: "Mr. Bean", clickCounter: 0 });

// Select property you want to observer
export const useSimpleStore = (select) => {
  return useSimpleState(store, select);
};

// Select multiple property you want to observer
export const useSimpeStoreMulti = (selectors) => {
  return useSimpleStateMulti(store, selectors);
};

Some component

// Component.js
import { useSimpeStore } from "./useSimpleStore";

const Component = () => {
  const [count, setCount] = useSimpeStore("clickCounter");
  
  return <div>
    <button onClick={() => setCount(count + 1)}>Incerment</button>
    <div>{count}</div>
  </div>
}

export default Component

Some other component

// Component.js
import { useSimpeStoreMulti } from "./useSimpleStore";

const Component = () => {
  const [{ name, clickCounter }, setSimpleState] = useSimpeStoreMulti([
    "name",
    "clickCounter"
  ]);
  
  return <div>
    <button onClick={() => setCount("name", "Gandalf the Grey"}>Set name</button>
    <button onClick={() => setCount("clickCounter", clickCounter + 1)}>Incerment</button>
    <div>name: {name}, clicks: {clickCounter}</div>
  </div>
}

export default Component

API

  • makeObservableSelect - initialize observable object, accept object to observe and return observable
const observable = makeObservableSelect(object);

| Param | Default | Required | Description | |---|---|---|---| | object | { } | yes | object to observe |

| Returns | Description | |---|---| | object | observable object |

  • useSimpleState - listen for change on observable
const [state, setSimpleState] = useSimpleState(selector: string, onChange?);

| Parame | Type | Default | Required | Description | |---|---|---|---|---| | selector | string | - | yes | property to oberve | | callback | fnction | - | no | onChange callback called with changed valueif provided hook will not rerender |

| Returns | Type | Description | |---|---|---| | state | unknown | observed value | | setSimpleState | function | update observed value |

  • useSimpleStateMulti - listen for change on observable with multiple selectors
const [state, setSimpleState] = useSimpleStateMulti(selectors: string[], onChange?);

| Parame | Type | Default | Required | Description | |---|---|---|---|---| | selectors | array | - | yes | properties to oberve | | callback | function | - | no | onChange callback called with property name and changed valueif provided hook will not rerender |

| Returns | Type | Description | |---|---|---| | state | object | observed values | | setSimpleState | function | update observed value |

:checkered_flag: TODO

  1. Add multiple selector :heavy_check_mark:
  2. Add reference equality check