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

hooksy

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hooksy

[demo](https://pie6k.github.io/use-state-shared/)

latest
npmnpm
Version
1.0.0
Version published
Weekly downloads
13
-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

use-state-shared

demo

Create custom useState hook that can be used inside multiple components and will share state across all of them (and will update all of them if state is changed)

API

import { createSharedStateHook } from 'use-state-shared';

// create custom `useState` hook and set default value only once. 
export const customUseState = createSharedStateHook(0);

// later import `customUseState` anywhere in the app

// use the same way as `useState` inside multiple different components. 

// `currentState` will be always synced between all of them. 
// changing it in any component will cause change in every component using it with updated value
const [currentState, setState] = customUseState();

Example

import { createSharedStateHook } from 'use-state-shared';

// create custom state hook with some initial value
// such hook can be created in some separated file and imported to many different components
const useGlobalCount = createSharedStateHook(0);
const useAnotherGlobalCount = createSharedStateHook(0);

function ComponentA() {
  const [globalCount, setGlobalCount] = useGlobalCount();
  const [anotherGlobalCount] = useAnotherGlobalCount();

  return (
    <div>
      I'm first component and count is {globalCount}. 2nd count is{' '}
      {anotherGlobalCount}.
      <button onClick={() => setGlobalCount(globalCount + 1)}>Increase</button>
    </div>
  );
}

function ComponentB() {
  const [globalCount, setGlobalCount] = useGlobalCount();
  const [anotherGlobalCount, setAnotherGlobalCount] = useAnotherGlobalCount();

  return (
    <div>
      I'm second component. Count is {globalCount}. This count will always be
      synced with ComponentA count.
      <button onClick={() => setGlobalCount(globalCount - 1)}>Decrease</button>
      <button onClick={() => setAnotherGlobalCount(anotherGlobalCount + 1)}>
        Increase 2nd count
      </button>
    </div>
  );
}

FAQs

Package last updated on 22 Jun 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