New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-use-global

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-global

React hook for using global state without unnecessary renders.

latest
Source
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

react-use-global

React hook for using global state without unnecessary renders.

Concept:

  • Create an global state object with initial value.
  • Subscribe a component to any part of that object.
  • The component will only re-render if that part of the state changes.

Install:

npm install react-use-global --save

Use:

APIusagedescription
function createGlobal(any initialState) => function useGlobalImported from 'react-use-global'. Takes initial state value, returns useGlobal function.
function useGlobal(string path) => [any state, function setState]Takes a path (e.g. 'user.settings.nightMode'), returns the value and a function to set that value. Just like React's useState.

Example:

import React from 'react'
import createGlobal from 'react-use-global'

const initialState = {
   count: 42,
   input: "wow this is cool",
   show: {
      count: false,
      input: true
   }
}
const useGlobal = createGlobal(initialState)

const Toggles = () => {
   const [showCount, setShowCount] = useGlobal('show.count')
   const [showInput, setShowInput] = useGlobal('show.input')
   return (
      <div>
        <input
            type="checkbox"
            checked={showCount}
            onChange={() => setShowCount(!showCount)}
        />
        <label>show count</label>
        <input
            type="checkbox"
            checked={showInput}
            onChange={() => setShowInput(!showInput)}
        />
        <label>show input</label>
      </div>
   )
}

const Count = () => {
   const [count, setCount] = useGlobal('count')
   const [showCount] = useGlobal('show.count')
   if (!showCount) return null
   return (
      <div>
         <span>{count}</span>
         <button onClick={() => setCount(count + 1)}>+</button>
         <button onClick={() => setCount(count - 1)}>-</button>
      </div>
   )
}

const Input = () => {
   const [input, setInput] = useGlobal('input')
   const [showInput] = useGlobal('show.input')
   if (!showInput) return null
   return (
      <div>
         <input
            value={input}
            onChange={(e) => setInput(e.target.value)}
         />
      </div>
   )
}

const App = () => {
   return (
      <div>
         <Toggles/>
         <Count/>
         <Input/>
      </div>
   );
}

export default App;

License

MIT

Keywords

react

FAQs

Package last updated on 03 Jul 2019

Did you know?

Socket

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