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

stafly

v0.0.3

Published

Stafly is a state management tool for react

Downloads

3

Readme

Stafly - Fly your state anywhere 🕊

Stafly is a state management tool for react

Let's start with a global state.

import { createStore } from "stafly";

const NameStore = createStore<string>();

const Ancenstor = props => {
  const name = NameStore.useValue();

  // rest component...
const Descendant = props => {
  const [name, setName] = useState("");

  NameStore.useSetValue(name);

  // rest component...

Done! We can get or set value from any component, either from up, down, or non-related components too

You might not need a global state is some cases (for example, if we use Ancestor component at several places), so sometimes we need to limit our flight. They say, sky is the limit, so let's wrap our Ancestor component

const Ancenstor = staflySky(NameStore)(props => {
  // rest component ...
});

That's it! Now we can render Ancenstor at multiple places and they will have separate states. No more singleton; however, keep in mind that if you use use stores in hierarchically higher places than components that are wrapped in sky, you will access global state there.

You can pass multiple stores to staflySky function. You can use staflySky.memo instead of staflySky to memoize component, just as React.memo would do.

Sometimes we might not need to have an up-to-date value of the store, but just access it whenever needed. In this case, Ancestor will not rerender on every change of value.

const Ancenstor = staflySky.memo(NameStore)(props => {
  const getName = NameStore.useValueGetterFn();

  const onSubmit = () => {
    const name = getName();
    // rest logic
  }

  // rest component...

You can get setter function too

const Ancenstor = props => {
  const setName = NamesStore.useValueSetterFn();
  
  // rest component...

When creating stafly, you can pass default value too:

const NameStore = createStafly({
  defaultValue: ""
});

You can add reducers to move state change logic away from components

const NameStore = createStafly({
  defaultValue: ""
}).addReducers({
  reset: (state) => "",
  addSuffix: (state, suffix: string) => state + suffix
});


const Ancestor = props => {

  const handleReset = NameStore.useDispatcher("reset");
  const handleAddPrefix = NameStore.useDispatcher("addSuffix");

  const addDollarSign = () => handleAddPrefix("$");

  const cleanup = () => {
    handleReset();
    // rest code
  }

You can subscribe to value change.

const Ancestor = props => {
    NameStore.useSubscriber((newName) => {
      // perform operations
    });

    // alternatively, if you want to manually control subscription and unsubscription, then you can write:

    const subscribe = NameStore.useSubscriberFn();

    useEffect(() => {
      const unsubscribe = subscribe(() => {
        // perform operations
      });

      return unsubscribe; // for cleanup
    }, [subscribe]);

    // rest component...

If you are saving complex and/or fast changing data in the store, sometimes you might not want to have up-to-date information for optimization purposes. You can not only get data when needed, but also set data in the store when requested. For that, you can register setter on the store.

const Descendant = props => {
  const [name, setName] = useState("");
  const inputRef = useRef();

  NamesStore.useRegisterValueSetter(() => inputRef.current.value);

  return <input defaultValue="" ref={inputRef} />

In this case, you will no longer have up-to-date value in the store, but when requested by value getter function, registered setter function will be called and you will get newest data. The value in the store will be updated too.

For advanced uses, such as accessing and modifying global state outside components, persisting global state, using immer, collecting data from multiple places and more, please see the rest of the documentation here.