This package has been deprecated

Author message:

Switch to @vighnesh153/react-use-global-state. No API changes

@vighnesh153/use-global-state
TypeScript icon, indicating that this package has built-in type declarations

1.3.120 • Public • Published

@vighnesh153/use-global-state

npm (scoped) Test Coveralls Coverage Status npm bundle size (scoped) npm peer dependency version (scoped) npm GitHub GitHub issues

An extremely lightweight library which is just 1KB minified that provides a React hook for having a piece of global state. It is similar to the React's useState hook but with the only addition that this hook persists the state globally.

This can be used as an alternative to the popular state management systems like Redux, MobX or even the built-in Context API. We can completely rely on hooks for handling the state for us.

Installation

npm install @vighnesh153/use-global-state

Usage

import useGlobalState from "@vighnesh153/use-global-state";

const Counter = ({ adder }) => {
  const [count, setCount] = useGlobalState('count', 0);
  
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + adder)}>
        Add {adder}
      </button>
    </div>
  );
};

const App = () => {
  return (
    <div>
      {/** Count state will be same for all counters **/}
      <Counter adder={1} />
      <Counter adder={2} />
      <Counter adder={3} />
    </div>
  );
};
Cleaning up for unit testing

You might want to clean up all the existing streams for your unit test suites. You can make use of the resetExistingStreams function to do that.

import { resetExistingStreams } from "@vighnesh153/use-global-state";

describe('Your component tests', () => {
  beforeEach(() => {
    resetExistingStreams();
  });
});

Examples

Counters Gif

Why you should use this library?

  • Size: 1KB minified
  • Zero external dependencies
  • Modern hook-based state management instead of the traditional redux like approaches
  • No need of wrapping components with a long chain of Providers as there is no Provider-Consumer pattern in this hook

Best practices

  • Try to keep the states very minimal. That way, it will be easier to make reusable and won't re-render lot of components just because some random sub-object changed in the state.
  • Although there is no restriction on how you want to use this, my recommendation would be to create a hook that has all the functionality related to the global state you want to track. This lets you encapsulate all the functionality related to the atomic state and will be easily scalable.
const useUser = (userId, initialValue) => {
  const [user, setUser] = useGlobalState(`user_${userId}`, initialValue || {});
  
  const changeName = useCallback((newName) => {
    setUser({ ...user, name: newName });
  }, [user]);

  const changeAge = useCallback((newAge) => {
    setUser({ ...user, age: newAge });
  }, [user]);
  
  return { user, changeName, changeAge };
};

How does this hook work?

  • This hooks makes use of the provided identifier to tap into its global stream.
  • When you change the state for an identifier, the new data gets published in the stream and all the components which are making use of that stream, will get the latest data.

Contributing

Pull requests are welcome. For any change, please open an issue first to discuss what you would like to change.

License

MIT

Package Sidebar

Install

npm i @vighnesh153/use-global-state

Weekly Downloads

7

Version

1.3.120

License

MIT

Unpacked Size

14.7 kB

Total Files

20

Last publish

Collaborators

  • vighnesh153