npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shared-state-hook

v1.2.7

Published

React Hook to share state without passing props or hierarchies

Downloads

27

Readme

shared-state-hook

npm (scoped) npm bundle size (minified)

This is a module for React that implements a hook named useSharedState for managing application state. It is similar to the provided hook useState, but rather than associating the state with the component that uses the hook, it stores the state outside of the component so it can be shared by many components.

Install

$ npm install shared-state-hook

Browser

<script src="//unpkg.com/shared-state-hook"></script>

https://unpkg.com/shared-state-hook

Example

import {useSharedState} from 'shared-state-hook'

const Counter = props => {
  
 const [count, setCount] = useSharedState("counter", props.count)
 
 return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  )
}
  
const CounterApp1 = props => <Counter count={1}/>
//Order matters, initial value has already been set
const CounterApp2 = props => <Counter count={2}/>

Try it on JSBin - CodeSandBox

API

useSharedState

import {useSharedState} from "shared-state-hook"

const Component = props => {
const [userInfo, setUserInfo] = useSharedState("userInfo", optionalInitialValue, optionalOnUpdatesCallback)
return ""
}
//=> "!"

useHooksOutside

Allows you to use React Hooks outside of the function body

Invariant React Error 307 image

import {useHooksOutside} from "shared-state-hook"

const ReactElement = useHooksOutside(()=>{
    //Call any restricted React Hook outside of a component function body! 
    useState()
    useEffect()
    useContext()
    useSharedState("userInfo", initialValues)
});
//=> "!"

You can initialize useSharedState from useHooksOutside as a external Provider that will both update the shared state by others as well as get notified if anyone else updates the same name share

let notifier
const rel = useHooksOutside(() => {
  const onUpdate = updates => {
    $scope.user = {
        ...$scope.user, ...updates
    }
    $scope.$apply()
  }
  
  const [, setUser] = useSharedState("user", $scope.user, onUpdate)
  notifier=setUser    
})

Forked from top-state-hook