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

@sethwebster/simple-state

v0.0.7

Published

A simple state management library for React

Downloads

21

Readme

React Simple State

Simple, global, high-performance state management for React.

...
import {useQuark} from '@sethwebster/simple-state';

function ComponentA() {
  const [ value, useQuark ] = useQuark('some-thing', 0);

  return (
    <div>
      <p>Value: {value}</p>
    </div>
  )
}

function ComponentB() {
  // Default value is ignored when declared a second time
  const [ value, useQuark ] = useQuark('some-thing', 0);

  return (
    <div>
      <p>Value: {value}</p>
      <button onClick={() => useQuark(value + 1)}>Increment</button>
    </div>
  )
}
...

Installation

npm i @sethwebster/simple-state

Usage

There are two primary apis:

  • useQuark - A hook that returns a value and a setter function
  • useDerivedQuark - A hook that returns a derived value based on other quark(s).

State is shared globally, but you can segment your data by using a context, <SimpleStateRoot>. This is useful if your data model organized in such a way that some is global to the whole app, and some is shared amongst a sub-section.

import { SimpleStateRoot } from '@sethwebster/simple-state';

function Products() {
  const products = getProducts();
  const [selectedProduct, setSelectedProduct] = useQuark('selected-product', products[0]);

  return (
    <ul>
      {products.map(product => (
        <li key={product.id}>
          <button onClick={() => setSelectedProduct(product)}>{product.name}</button>
        </li>
      ))}
    </ul>
  )
}

function Nav() {
  const [selectedTab, setSelectedTab] = useQuark("selected-tab", "products");
}

function App() {
  return (
    {/* The state here is separate from that state within the <SimpleStateRoot /> below */}
    <Header>
      <Nav selectedTab={selectedTab} />
    </Header>
    {/* new state context */}
    <SimpleStateRoot>
      <Products />
    </SimpleStateRoot>
  )
}

API

useQuark

const [value, setValue] = useQuark<T>(key: string, initialValue: T): [T, (newValue: T) => void]

This hook is strongly typed and returns a value and a setter function. The setter function is memoized, so it can be passed to child components without causing unnecessary re-renders if desired. However, this is not strictly necessary as it's all the same shared state.

useDerivedQuark

const value = useDerivedQuark<T>(
   key: string, 
   selector: (({get: (key: string): ?}) => T)
): T

This hook is strongly typed and returns a value derived using the selector function.

import { useQuark, useDerivedQuark } from '@sethwebster/simples-state';

function ComponentA() {
  const [value, setValue] = useQuark('some-thing', 10);
  const [otherValue, setOtherValue] = useQuark('some-other-thing', 15);

  // Update the values in some way
  useEffect(()=>{
    const interval = setInterval(() => {
      setValue(value + 5);
      setOtherValue(otherValue + 10);
    }, 1000)
  },[]);

  ...
}

function DerivedComponentB() {
  const value = useDerivedQuark('some-key-derived', ({get}) => {

    // Run when either value changes, and this component re-renders
    const someThing = get<number>('some-thing');
    const someOtherThing = get<number>('some-other-thing');

    return someThing * someOtherThing;
  })  
  
  return <div>{value}</div>
}
// 0s
// <div>150</div>
// 1s
// <div>375</div>
...
<TReturn>({get<T>: (key: string) => T}) => TReturn

License: MIT