Socket
Socket
Sign inDemoInstall

useglobalstore

Package Overview
Dependencies
6
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    useglobalstore

global state management with hooks and Context API


Version published
Maintainers
1
Created

Readme

Source

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>
        )
    }

FAQs

Last updated on 07 May 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc