Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-hook-shared-state

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-hook-shared-state

A simple way to create shared state using the hook API from React.

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-hook-shared-state

A simple way to create shared state using the hook API from React.

Requires at least React 16.7.0

Install

yarn add react-hook-shared-state

Example

import createSharedState from 'react-hook-shared-state'

const [useSharedState, setSharedState] = createSharedState('initial state')

/*
 * The state of every component will be assigned the same value.
 */
const MyComponent = () => {
  const state = useSharedState()
  return (
    <div
      onClick={() => setSharedState(state => state.replace('initial', 'new'))}
    >
      {state}
    </div>
  )
}

API

createSharedState(initialValue)

Returns a tuple with two functions in this order:

  • useSharedState() - Use this in your component. This will return the current state and cause your component to rerender when a new state is set.
  • setSharedState(nextState | (previousState) => nextState) - Call this function to set the new shared state. You can also pass a function which will receive the previous state as a parameter and returns the next state.

Keywords

FAQs

Package last updated on 24 Jan 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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc