npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

usesync

v2.0.2

Published

A subscription based state management solution for React!

Downloads

13

Readme


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