1.0.11 • Published 2 years ago

use-super-state v1.0.11

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

useSuperStore 🌟

The React state management library made by me for myself.

Installation

  1. Install the npm package with npm i use-react-state

  2. Render SuperStateProvider somewhere near the top of the component tree

import { SuperStateProvider } from "use-super-state";

// ...

export default function App() {
  return (
    <SuperStateProvider>
      <Router />
    </SuperStateProvider>
  );
} 
  1. Add useComputedSuperState to the list of additionalHooks in ESLint config (recommended).
{
  "eslintConfig": {
    "extends": [
      "react-app"
    ],
    "rules": {
      "react-hooks/exhaustive-deps": [
        "warn",
        {
          "additionalHooks": "(useComputedSuperState)"
        }
      ]
    }
  }
}

API

// Create store
const superState = createSuperState(() => ({ counter: 0 }));

// Live selector forces re-render whenever the state changes 
const [counter, setIncrement, getIncrement] = useSuperState(superState.counter).live;

// Lazy selector never forces re-render 
const [getIncrement, setIncrement] = useSuperState(superState.counter).lazy;

// Get or set any field in any store. Useful e.g. in callbacks if they involve many 
// or dynamically determined fields.
const [getValue, setValue] = useLazySuperState();

const counter = getValue(superState.counter);
setValue(superState.counter, 12);

const [getCounter, incrementCounter] = useComputedSuperState(
  () => getValue(counterStore.counter),   // Selector
  [getField],                             // Dependencies of selector (like in useMemo or useCallback)
  increment                               // Memoized version of setter function (not required)
);

function onClick() {
  // Synchronously update a few fields in batch so that it triggers change listeners just once  
  superAction(() => {
    setValue(superState.counter, 15);
    
    // Warning: superState.counter is not yet updated at this point!
    setValue(superState.counter, 20);
  }) 
}

License

MIT

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago