GithubHelp home page GithubHelp logo

lucamattiazzi / use-synced-state Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 242 KB

React hook to use state synced to another react remote/local state

HTML 9.99% JavaScript 25.25% CSS 1.52% TypeScript 63.23%

use-synced-state's Introduction

use-synced-state

NPM JavaScript Style Guide

What is this?

Ever needed to sync a state between two instances of a React app? Well, you and I have something in common, not everyone has ever needed to do shit like this!

Anyway, this is a simple hook that allows you to create a state/setter that will be immediatly synced to a local/remote source via BroadcastChannel (local) or Firebase (remote), or any sync method you like!

When creating a hook you need to provide a source: an object with onData and sendData, but worry not! a source generator for BroadcastChannel and Firebase is provided with the library, who would have thought???

You can use a hook as receiver, as sender or as both (default) in order to limit a component to mimic another state, or for the synchronization to work both ways.

Install

npm install --save use-synced-state

Usage

Local source, two end sync:

import * as React from 'react'

import { useSyncedState, localSource } from 'use-synced-state'

const source = localSource()
const App = () => {
  const [value, setValue] = useSyncedState('counter', altSource, 1)
  return (
    <div onClick={() => setValue(value + 1)}{value}</div>
  )
}
export default App

Remote source, one direction sync:

import * as React from 'react'

import { useSyncedState, firebaseSource } from 'use-synced-state'

const firebaseConfig = {
  apiKey: 'whatanapikey',
  authDomain: 'maronn.firebaseapp.com',
  databaseURL: 'https://maronn-default-rtdb.europe-west1.firebasedatabase.app',
  projectId: 'maronn',
  storageBucket: 'maronn.appspot.com',
  messagingSenderId: '100000000',
  appId: '1:123:456',
  measurementId: 'hoooge',
}

const source = firebaseSource(firebaseConfig, 'databaseName')
const role = window.location.hash === '#receiver' ? 'receiver' : 'sender'
const App = () => {
  const [value, setValue] = useSyncedState('counter', altSource, 1, role)
  return (
    <div onClick={() => setValue(value + 1)}{value}</div>
  )
}
export default App

License

MIT © lucamattiazzi


This hook is created using create-react-hook.

use-synced-state's People

Contributors

lucamattiazzi avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.