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

set-state-is-great

v0.0.23

Published

Global store + setState + hooks integration

Downloads

2

Readme

2022 update: React 18, useId, cleaning house

Set State is Great

Global state management without the ceremony. Zero dependency (other than React of course). No Context or reducers.

Installing

npm install set-state-is-great

or

yarn add set-state-is-great

Creating the store

Set State is Great (SSiG) is, at its core, just a key/value store.

import { Store } from 'set-state-is-great';

const appState = {
  viewShown: 'Home',
  colormode: 'dark',
  drawer: { open: false, other: '?' },
};

const store = new Store(appState);

setState & setPartialState

For mutating a store's data, there's setState & setPartialState:

setState replaces the state for a key:

store.setState('drawer', { open: true, other: 'yup' });

Use setPartialState for partial updates to objects, it will assign (via Object.assign) the new values to the existing object:

store.setPartialState('drawer', { open: true });

useStoreState

SSiG's main hook. Use it to watch for changes to a particular key.

import { store } from './globals';
import { useStoreState } from "set-state-is-great";

function Drawer() {
  const { open } = useStoreState(store, 'drawer');

  return (
    <MuiDrawer open={open}>
      <div>just drawer things</div>
    </MuiDrawer>
  );
}

export default Drawer;

useNonNullState

The other hook - works just like useStoreState, but checks that the returning value is not null or undefined (and throws an error if it is). Returning value is set to NonNullable.

store.state

Access the central state obj via store.state.

Feel free to mutate it as you see fit.

store.state.drawer; // => {open: true, other: 'yup'}
store.getNonNullState('drawer'); // throws an error if null or undefined
store.state.drawer.open = false;
store.forceUpdate('drawer');

Or just replace it wholesale:

store.state = {
  viewShown: 'Home',
  colormode: 'light',
  drawer: { open: false, other: '?' },
};

Force updating components

// forceUpdate all components watching a particular key
store.forceUpdate('drawer');

setStateIfDifferent

setStateIfDifferent will only rerender watching components if the value differs. EG:

store.setStateIfDifferent('breakpoint', 'sm');`

Organizing the store (and some TypeScript)

How I do it: create a constants.ts file with a store variable and function to set it:

// constants.ts
import { Store } from "set-state-is-great";
import { AppState } from "./types";

export var store: Store<AppState>;

export const setStore = (theStore: Store<AppState>) => {
  store = theStore
  window.App = { store: theStore }
}

Then set it when creating the store:

// store.ts
import { Store } from 'set-state-is-great';
import { AppState } from './types';
import { setStore } from './globals';

const appState: AppState = {
  drawer: { open: false, other: '?' },
  modal: { open: false, title: 'nada' },
}

const store = new Store<AppState>(appState);

setStore(store);

Then you import the store from any file: import { store } from './globals';

getHelpers

getHelpers gives you the following functions scoped to a particular key:

setState, setPartialState, setStateIfDifferent

import { store } from './globals';

const { setPartialState } = store.getHelpers('drawer');

const closeDrawer = () => {
  setPartialState({ open: false });
};

TypeScript

SSiG is written in & optimized for TS, and it's highly recommended that you use it with TS.

To do so, first define your store's state:

type DrawerState = {
  open: boolean;
  other: string;
}

type ModalState = {
  open: boolean;
  title: string;
}

export type AppState = {
  colormode: 'dark' | 'light';
  drawer?: DrawerState;
  modal?: ModalState;
}

Then pass in AppState as a Generic when creating your store:

const store = new Store<AppState>({ colormode: "dark" });

Now setState et al. will check that you're passing in the correct types.