A userland React hook for managing global state (#snippet)

Yoav Kadosh shared a very useGlobalState React hook.
const store = {};
const listeners = {};

function useGlobalState(key, initialValue) {
const [state, _setState] = useState(store[key] || initialValue);
const setState = useCall…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Yoav Kadosh shared a very useGlobalState React hook.

const store = {};
const listeners = {};

function useGlobalState(key, initialValue) {
  const [state, _setState] = useState(store[key] || initialValue);
  const setState = useCallback((stateOrSetter) => {
    let next = stateOrSetter;
    if (typeof stateOrSetter === "function") {
      next = stateOrSetter(store[key]);
    }

    listeners[key].forEach((l) => l(next));
    store[key] = next;
  }, []);
  useEffect(() => {
    // Store the initial state on the first call with this key
    if (!store[key]) {
      store[key] = initialValue;
    }
    // Create an empty array of listener on the first call with this key
    if (!listeners[key]) {
      listeners[key] = [];
    }
    // Register the observer
    const listener = (state) => _setState(state);
    listeners[key].push(listener);
    // Cleanup when unmounting
    return () => {
      const index = listeners[key].indexOf(listener);
      listeners[key].splice(index, 1);
    };
  }, []);
  return [state, setState];
}

You can then use the defined useGlobalState hook to share or update application state across components. 💪

const [state, setState] = useGlobalState('someUniqueKey', INITIAL_STATE);

Are there downsides to this pattern? I don't know, and I'm a little puzzled why I haven't seen this userland hook before, but if you think useGlobalState is a bad idea, let me know!

If you want to learn more, Yoav explains how it works on his blog!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2023-02-17T23:00:00+00:00) A userland React hook for managing global state (#snippet). Retrieved from https://www.scien.cx/2023/02/17/a-userland-react-hook-for-managing-global-state-snippet/

MLA
" » A userland React hook for managing global state (#snippet)." Stefan Judis | Sciencx - Friday February 17, 2023, https://www.scien.cx/2023/02/17/a-userland-react-hook-for-managing-global-state-snippet/
HARVARD
Stefan Judis | Sciencx Friday February 17, 2023 » A userland React hook for managing global state (#snippet)., viewed ,<https://www.scien.cx/2023/02/17/a-userland-react-hook-for-managing-global-state-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » A userland React hook for managing global state (#snippet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/17/a-userland-react-hook-for-managing-global-state-snippet/
CHICAGO
" » A userland React hook for managing global state (#snippet)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2023/02/17/a-userland-react-hook-for-managing-global-state-snippet/
IEEE
" » A userland React hook for managing global state (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2023/02/17/a-userland-react-hook-for-managing-global-state-snippet/. [Accessed: ]
rf:citation
» A userland React hook for managing global state (#snippet) | Stefan Judis | Sciencx | https://www.scien.cx/2023/02/17/a-userland-react-hook-for-managing-global-state-snippet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.