use-synced
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

use-synced NPM version NPM monthly downloads NPM total downloads

Keeping different tabs of your React app synced has never been this easy!!! 😄

Please consider following this project's author, Sina Bayandorian, and consider starring the project to show your ❤️ and support.

A simple React hook that provides you with a state and its setState function that is synced between all tabs of your application. With support for the debounce functionality and fully type-safe as it's written in TypeScript. 🚀

Table of Contents

Install

Install with npm:

$ npm install use-synced --save

Usage

import useSynced from 'use-synced';

const MyComponent = () => {
  const [count, setCount] = useSynced('count', 0);
  // the count state is synced between different tabs of your app

  return (
    <div>
      <button onClick={() => setCount(prev => ++prev)}>Increment</button>
      { count } 
    </div>
  );
};

export default MyComponent;

Hook

useSynced

returns a state that is synced between different tabs of the same application - utilizes localstorage or sessionstorage behind the scenes and can be configured to best fit the needs of your application

Params

  • key : String - the key that is used to store the value in local/sessionstorage

  • initialValue : Any - the initial value of the state is used if the value is not already stored

  • options : Object - additional configuration of the hook

    • type : 'local' | 'session' - optional - defaults to 'local' - type of the storage used to sync the state

    • delay : Number - optional - defaults to undefined - the delay after which the tabs are synced - delay of the debounced sync function

Returns

  • [syncedState, setSyncedState] : [Any, Function]

License

MIT License

Copyright (c) 2024 Sina Bayandorian

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Package Sidebar

Install

npm i use-synced

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

16.3 kB

Total Files

9

Last publish

Collaborators

  • sina_byn