GithubHelp home page GithubHelp logo

imrdjai / usesync Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 38 KB

A subscription based state management solution for React!

License: MIT License

JavaScript 100.00%
react hook state react-hook react-state usesync react-hooks hooks state-management

usesync's Introduction


useSync

npm

A subscription based state management solution for React!


Table of Contents

Installation:

Install the package from npm:

$ npm install usesync --save

Then simply require it:

const { useSync, sync, storage } = require("usesync") // CJS

or

import useSync, { sync, storage } from "usesync" // ESM

Usage:

useSync(id: string, initialValue?: any): any

This is the hook that you will use across your React components, it allows them to subscribe to a specific sync with the ID you give:

const Component = () => {
    useSync('hello')
    return (
        <div>
            Hello World!
        </div>
    )
}

Subscribing to muiltiple syncs is possible, just call the hook multiple times with different IDs:

const Component = () => {
    useSync('id num 1')
    useSync('id num 2')
    return (
        <div>
            Hello World!
        </div>
    )
}

Updating sync IDs at runtime is allowed:

const GreetUser = (id) => {
    const user = getUser(id)
    useSync(`Users ${id}`)
    return (
        <div>
            Hello {user.firstName}!
        </div>
    )
}

To synchronize state in multiple components simply call the hook inside all of them and give the same ID:

const ComponentA = () => {
    const name = useSync('hello', 'World')
    return (
        <div>
            Hello {name}! (A)
        </div>
    )
}
const ComponentB = () => {
    const name = useSync('hello', 'World')
    return (
        <div>
            Hello {name}! (B)
        </div>
    )
}

sync(id: string, newValue?: any): void

The hook alone does nothing, to dispatch a sync you need to call this function, this will cause all the subscribed components to re-render. Here is an example:

import React from 'react'
import ReactDOM from 'react-dom'
import useSync, { sync } from "usesync"


const ComponentA = () => {
    useSync('Components')
    return (
        <div>
            Random Number (A): {Math.random()}
        </div>
    )
}
const ComponentB = () => {
    useSync('Components')
    useSync('ComponentB')
    return (
        <div>
            Random Number (B): {Math.random()}
        </div>
    )
}
const ComponentC = () => {
    useSync('Components')
    return (
        <div>
            Random Number (C): {Math.random()}
        </div>
    )
}
const App = () => {
    const handleClick = () => {
        sync('Components')
    }
    const handleClick2 = () => {
        sync('ComponentB')
    }
    return (
        <div>
            <ComponentA />
            <ComponentB />
            <ComponentC />
            <ComponentC />
            <button onClick={handleClick}>re-render all</button>
            <button onClick={handleClick2}>re-render component b</button>
        </div>
    )
}

ReactDOM.render(<App />, document.getElementById('app'))

Try it on CodePen!

New: You may pass a sync value to this function, and access it from the subscribed components:

import React from 'react'
import ReactDOM from 'react-dom'
import useSync, { sync } from "usesync"


const initialValue = Math.random()

const ComponentA = () => {
    const randomNumber = useSync('Components', initialValue)
    return (
        <div>
            Random Number (A): {randomNumber}
        </div>
    )
}
const ComponentB = () => {
    const randomNumber = useSync('Components', initialValue)
    return (
        <div>
            Random Number (B): {randomNumber}
        </div>
    )
}
const ComponentC = () => {
    const randomNumber = useSync('Components', initialValue)
    return (
        <div>
            Random Number (C): {randomNumber}
        </div>
    )
}
const App = () => {
    const handleClick = () => {
        sync('Components', Math.random())
    }
    return (
        <div>
            <ComponentA />
            <ComponentB />
            <ComponentC />
            <ComponentC />
            <button onClick={handleClick}>re-render all</button>
        </div>
    )
}

ReactDOM.render(<App />, document.getElementById('app'))

Try it on CodePen!

storage: Object

This is an optional object that is globaly available across your app, it can be used to store states for your componenets. Here is an example:

import React from 'react'
import ReactDOM from 'react-dom'
import useSync, { sync, storage } from "usesync"


storage.randomNumber = Math.random()

const ComponentA = () => {
    useSync('Components')
    return (
        <div>
            Random Number (A): {storage.randomNumber}
        </div>
    )
}
const ComponentB = () => {
    useSync('Components')
    useSync('ComponentB')
    return (
        <div>
            Random Number (B): {storage.randomNumber}
        </div>
    )
}
const ComponentC = () => {
    useSync('Components')
    return (
        <div>
            Random Number (C): {storage.randomNumber}
        </div>
    )
}
const App = () => {
    const handleClick = () => {
        storage.randomNumber = Math.random()
        sync('Components')
    }
    const handleClick2 = () => {
        storage.randomNumber = Math.random()
        sync('ComponentB')
    }
    return (
        <div>
            <ComponentA />
            <ComponentB />
            <ComponentC />
            <ComponentC />
            <button onClick={handleClick}>re-render all</button>
            <button onClick={handleClick2}>re-render component b</button>
        </div>
    )
}

ReactDOM.render(<App />, document.getElementById('app'))

Try it on CodePen!

Notes

Give this cool project a star ⭐! I will appreciate it ❤

GitHub Repo stars

License

MIT © iMrDJAi

usesync's People

Contributors

imrdjai avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.