GithubHelp home page GithubHelp logo

gus3inov / use-share-state Goto Github PK

View Code? Open in Web Editor NEW
15.0 0.0 2.0 315 KB

React hook for share state between tabs/windows

License: MIT License

HTML 20.92% CSS 15.26% JavaScript 27.15% TypeScript 36.67%

use-share-state's Introduction

React Shared State Hook 🔗

React hook for state synchronization between (windows, tabs).

This hook is based on a broadcast-channel library that works in New Browsers, Old Browsers, WebWorkers and NodeJs.

Installation

$ npm install use-share-state

Usage

import useShareState from "use-share-state";

const App = () => {
  const [state, setState] = useShareState(0);

  const increment = useCallback(() => {
    setState(state + 1);
  }, [state]);

  const decrement = useCallback(() => {
    setState(state - 1);
  }, [state]);

  return (
    <div className="App">
      Counter: {state}
      <button onClick={increment}>+</button>
      <button onClick={decrement}>-</button>
    </div>
  );
}

License

MIT © Muslim Guseinov

use-share-state's People

Contributors

gus3inov avatar

Stargazers

Nikita avatar  avatar  avatar Masiya Guseynov avatar Jonathan Lovera avatar James Anthony Bruno avatar Roman Hossain Shaon avatar Nick Beattie avatar Michael Demarais avatar Christoph Jerolimov avatar Philipp Brumm avatar Faiyaz Shaikh avatar Miguel Cast avatar Adnan Topal avatar Sergio Xalambrí avatar

use-share-state's Issues

Using in multiple components creates multiple instances of BroadcastChannel

Given that BroadcastChannel instance is inherently a client of a single store shared among both windows, tabs, and, in the case of reactjs, multiple components within a single tab.

I Haven't done any analysis if multiple instances of BroadcastChannel in a single JS context are a good or bad thing. So, I'm not sure if evolving it to the React context pattern would make any difference other than increased complexity.

If you happen to have more insight, please share and I'm willing to take a stab at this idea.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.