useglobalstore
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

useGlobalStore

Global state management with hooks and Context API

Usage

Create provider

Create your provider component based on your main reducer

    import { createStore } from 'useglobalstore'
    import { myReducer } from 'my/reducer'
    
    export const Store = createStore(myReducer)

Provide the store

Just wrap your main component with the provider created before

    import React from 'react'
    import { Store } from 'my/store'
    import { MyComponent1, MyComponent2 } from 'my/components'
    
    export const App = () => {
        return (
            <Store>
                <MyComponent1 />
                <MyComponent2 />
            </Store>
        )
    }

Use store locally

Finally use useStore hook to access global store data and dispatch method

    import React from 'react'
    import { useStore } from 'useglobalstore'
        
    export const MyInnerComponent = () => {
    
        const { data, dispatch } = useStore()
    
        const onClick = () => {
            dispatch({ type: 'MY_ACTION' })
        }
    
        return (
            <span onClick={onClick}>{data}</span>
        )
    }

Readme

Keywords

none

Package Sidebar

Install

npm i useglobalstore

Weekly Downloads

3

Version

0.0.2

License

ISC

Unpacked Size

6.43 kB

Total Files

5

Last publish

Collaborators

  • matteozanoncello