DEV Community

Dr. Sergey Pogodin
Dr. Sergey Pogodin

Posted on

Global State in React

A year ago I engineered my own React library for the global state and asynchronous data management with Context API and hooks (yeah, yet another take on such library :) It worked great for me ever since, but nobody else knows it. Recently I got an inspiration to write a little article / tutorial about it, and share it with the world, to see if anybody else likes it, and probably to receive some feedback. Check it out:

NPM: https://www.npmjs.com/package/@dr.pogodin/react-global-state

Article: https://dr.pogodin.studio/dev-blog/the-global-state-in-react-designed-right

P.S.: Why this one is better than a myrriad of alternatives? It allows to load some remote data into some.path of global state with a simple hook

const { data, loading, timestamp } = useAsyncData('some.path', asyncLoaderFunction)
Enter fullscreen mode Exit fullscreen mode

and takes care to provide a way for it to work in Server-Side Rendering (SSR), and within split code chunks. And the simple synchronous usage of global state works with a hook closely mimicing the standard local state:

const [value, setValue] = useGlobalState('some.path.in.state', 'Optional initial value (can also be a number, an object, on an array)')
Enter fullscreen mode Exit fullscreen mode

So far I have not seen all the same functionality in any alternative lib.

Top comments (0)