g-store

1.0.5 • Public • Published

g-store

A Simplified Global store for React

Installation

npm install g-store

Usage

In the index.js file of the react app

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {Provider} from 'g-store';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Provider>
    <App />
  </Provider>
);

In You can use it in your Component

import { useGStore } from 'g-store'; 

function App() {
  const [state, setState] = useGStore(0,"UniqueGlobalStateName");
  
 const increment1=()=>{
    const tempState=state+1
    setState(tempState)
  }
  return (
    <>
     <button onClick={increment}>increment</button>
     <div >{state}</div>
    </>
  );
}

export default App;

Limitation

  1. You can use setState of useGStore to update the state only like this

      const tempState=state+1
        setState(tempState)

    You can't use like below

        setState((state)=>state+1)
  2. You have to pass a unique name to each state as a second argument in useGStore

     const [userName, setUserName] = useGStore("initial Value","userName");
      const [age, setAge] = useGStore(20,"age");

License

MIT

Dependents (0)

Package Sidebar

Install

npm i g-store

Weekly Downloads

2

Version

1.0.5

License

MIT

Unpacked Size

4.32 kB

Total Files

7

Last publish

Collaborators

  • arun-me