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

react-state-sync

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-state-sync

A custom React hook for synchronized state

latest
Source
npmnpm
Version
1.2.1
Version published
Maintainers
1
Created
Source

React State Sync - a custom hook for synchronizing state

Create synchronized state instances, then use them throughout your app. Opt into localStorage or sessionStorage persistence to sync the state across multiple tabs.

✨ Basic usage

import { createSyncState } from 'react-state-sync';

// create a synchronized state instance
const counter = createSyncState({ defaultValue: 0 });

// extract the getter custom hook and an updater function
const { useSyncValue, setSyncValue } = counter;

function CounterDisplay() {
  // get the count value
  const count = useSyncValue();
  
  return <div>Count is: {count}</div>;
}

function DoubleDisplay() {
  // get the count value and transform it
  const doubleCount = useSyncValue(n => n * 2);

  return <div>Double is: {doubleCount}</div>;
}

function Counter() {
  return <button onClick={() => setSyncValue(n => n + 1)}>increment</button>;
}

function Reset() {
  return <button onClick={() => setSyncValue(0)}>reset</button>;
}

function App() {
  return (
    <>
      <CounterDisplay />
      <DoubleDisplay />
      <Counter />
      <Reset />
    </>
  );
}

With localStorage

Persisting the state to a storage layer will sync your state across multiple opened tabs.

First, initialize the state instance with the storage option set to window.localStorage and give it a unique key name:

// in ./sharedState
export const counter = createSyncState({
  defaultValue: 0,
  storage: window.localStorage,
  key: 'counter',
});

Then just use the state as normal in one tab and watch your app get updated in the other tabs:

import { counter } from './sharedState';

const { useSyncValue, setSyncValue } = counter;

// part of the app rendering in a browser tab
function Counter() {
  const count = useSyncValue();
  return <button onClick={() => setSyncValue(n => n + 1)}>increment {count}</button>;
}
import { counter } from './sharedState';

const { useSyncValue } = counter;

// part of the app rendering in a different tab
function CounterDisplay() {
  const count = useSyncValue();
  
  return <div>Count is: {count}</div>;
}

Keywords

synchronized

FAQs

Package last updated on 01 Dec 2021

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