8000 GitHub - axtk/groundstate: Minimalist shared state management for React apps
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

axtk/groundstate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm Lightweight TypeScript ✓ CSR ✓ SSR ✓

groundstate

Minimalist shared state management for React apps

  • Similar to useState()
  • No boilerplate
  • Painless transition from local state to shared state and vice versa
  • SSR-compatible

Installation: npm i groundstate

Usage

Moving the local state to the full-fledged shared state:

  import {createContext, useContext} from 'react';
+ import {Store, useStore} from 'groundstate';
+
+ let AppContext = createContext(new Store(0));

  let Counter = () => {
-     let [counter, setCounter] = useState(0);
+     let [counter, setCounter] = useStore(useContext(AppContext));

      let handleClick = () => {
          setCounter(value => value + 1);
      };

      return <button 
  };

  let ResetButton = () => {
-     let [, setCounter] = useState(0);
+     let [, setCounter] = useStore(useContext(AppContext), false);

      let handleClick = () => {
          setCounter(0);
      };

      return <button 
  };

  let App = () => <><Counter/>{' '}<ResetButton/></>;

Live demo

🔹 The Groundstate's shared state setup is very similar to useState() allowing for quick migration from local state to shared state or the other way around.

🔹 The false parameter in useStore(store, false) (as in <ResetButton> above) tells the hook not to subscribe the component to tracking the store state updates. The common use case is when the component doesn't make use of the store state value, but it may use the state setter.

🔹 An application can have as many stores as needed, whether on a single React Context or multiple Contexts.

let AppContext = createContext({
    users: new Store(/* ... */),
    items: new Store(/* ... */),
});

🔹 Apart from a boolean, useStore(store, shouldUpdate) can take a function of (nextState, prevState) => boolean as the second parameter to filter store updates to respond to:

let ItemCard = ({id}) => {
    let hasRelevantUpdates = useCallback((nextItems, prevItems) => {
        return nextItems[id].revision !== prevItems[id].revision;
    }, [id]);

    let [items, setItems] = useStore(
        useContext(AppContext).items,
        hasRelevantUpdates,
    );

    return (
        // content
    );
};

🔹 Shared state can be provided to the app by means of a regular React Context provider:

  let App = () => (
-     <AppContext.Provider value={42}>
+     <AppContext.Provider value={new Store(42)}>
          <PlusButton/>{' '}<Display/>
      </AppContext.Provider>
  );

🔹 A store can contain data of any type.

Live demos:
Primitive value state
Object value state

🔹 Immer can be used with useStore() just the same way as with useState() to facilitate deeply nested data changes.

Live demo with Immer

🔹 A store initialized outside a component can be used as the component's remount-persistent state.

0