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

simstate

v3.0.1

Published

A strongly-typed React state management tool favoring React Hooks

Downloads

1,254

Readme

simstate

npm types Build Status Coverage Status

simstate is a strongly-typed React state management tool favoring React Hooks and TypeScript.

Install

npm install --save simstate

How to use

Talk is cheap. Here is the code. :smile:

import React, { useState, useCallback, useRef } from "react";
import { useStore, StoreProvider, createStore } from "simstate";

// 1. Define your stores as a custom hook.
//    Any hook supported by React is supported here!
function CounterStore(initialValue: number) {
  const [value, setValue] = useState(initialValue);

  const incrementStep = useRef(1).current;
  const increment = useCallback(() => setValue(value + incrementStep), [value]);

  return { value, setValue, increment };
}

// 2. Create a store (Arguments as parameters to the function)
//    Arguments are type-checked so never pass bad arguments!
const counterStore = createStore(CounterStore, 42);

// 3. Wrap your component with StoreProvider
const RootComponent: React.FC = () => (
  <StoreProvider stores={[counterStore]}>
    <MyComponent />
  </StoreProvider>
);

// 4. Pass the store function to useStore to get the store
const MyComponent: React.FC = () => {
  const counterStore = useStore(CounterStore);

  return (
    <div>
      <p>Current: <span>{counterStore.value}</span></p>
      <button onClick={counterStore.increment}>Increment</button>
      <button onClick={() => counterStore.setValue(counterStore.value - 1)}>Decrement</button>
    </div>
  );

Features

  • Simple APIs and you just read all of them
  • No dependency but React 16.8 or higher and tslib for TypeScript projects
  • Strongly typed with TypeScript, and all types can be inferred.
  • Nested providers. Inner provided stores will override outer stores

v2

Since v3, simstate has been revamped to fully embrace react hooks. Looking for more traditional usage and implementation? Check out v2 branch.

Tips

  • Check examples for more usage
  • Name your store with capital letter (AStore), and name with store instance with lowercase letter (aStore). It makes it easy to work with multiple stores in one component.
const MyComponent = () => {
  const aStore = useStore(AStore);
  const bStore = useStore(BStore);
  // ...
}
  • Store instance is immutable. Every time a state is changed, a new store instance is created. Therefore, when store instance is used in combination with your custom hook, make sure that store instance is one of your hook deps. (examples/immutable_store_instance.tsx)

Related

Simstate and Why (English): This article talks about why I write this library. simstate-i18n: A Strongly-typed React i18n Library based on simstate.

Credits

unstated-next - This tool is hugely inspired by unstated-next.

outstated - The implementation looks alike.

License

MIT © ddadaal