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

@emab/react-state-sync

v0.1.7

Published

![npm (scoped)](https://img.shields.io/npm/v/@emab/react-state-sync?style=flat-square) ![npm](https://img.shields.io/npm/dw/@emab/react-state-sync?style=flat-square) ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/emab/reac

Downloads

4

Readme

React State Sync - simple state syncing in React

npm (scoped) npm GitHub Workflow Status GitHub

This package provides a simple way to share state across:

  • Components in the same app
  • Components in different apps
  • Components in different tabs

It uses the BroadcastChannel API to broadcast state changes and synchronize them between different instances of your React application. The hook also gives you the ability to perform optimistic updates, providing you with rollback functionality if needed.

Demo

https://emab.github.io/react-state-sync/

The demo app is made up of two separate React apps on one page, featuring a simple counter that can be incremented and decremented. The state is synced between the two apps, and the optimistic update functionality is used to perform the increment/decrement operations.

As well as the counter, there is also a fake async version. It randomly fails 50% of the time, and the optimistic update functionality is used to rollback the state if the operation fails. If the operation is successful, the new value is synced across the two apps.

You can also open the site in multiple tabs - if a change is made in one tab its values will be synced across the other tabs.

Note: one tab will override the values in another if it is opened and a new value is broadcast.

Usage

To use the useSyncedState hook, import it into your component:

import {useSyncedState} from "react-state-sync";

Then call the hook in your functional component:

const [value, syncValue, updateValueOptimistically] = useSyncedState("stateKey", initialValue);

You can provide a State type which will be used to type the value returned by the hook:

type State = {
    count: number;
};

const [value, syncValue, updateValueOptimistically] = useSyncedState<State>("count", 0);

Parameters:

  • stateKey (string): A unique key that identifies the state to be synced across multiple tabs or windows.
  • initialValue (T): The initial value for the state.
  • options: (Options): An optional object containing options for the hook.

Return Values:

  • value (T): The current state value.
  • syncValue (TVoidFunction): Function to sync the state. This will also broadcast the new state to other tabs/windows.
  • updateValueOptimistically (TOptimisticUpdateFunction): Function that updates the state optimistically, returning an object with rollbackValue and syncValue functions.

Using syncValue:

To update the state value and broadcast the new state to other tabs/windows, call syncValue:

syncValue(newValue);

Using updateValueOptimistically:

To perform an optimistic update, call updateValueOptimistically:

const {rollbackValue, syncValue} = updateValueOptimistically(newValue);

This function will immediately update the state and return two functions:

  • rollbackValue: Call this function if you want to roll back the state to the value it had before the optimistic update.
  • syncValue: Call this function when you're sure you want to commit the optimistic update. You can optionally pass a new value to sync.

For example:

const handleOptimisticUpdate = async (newValue) => {
    const {rollbackValue, syncValue} = updateValueOptimistically(newValue);

    try {
        // Replace this with your actual API request or some operation that could fail
        await fakeApiRequest();
        syncValue(); // sync the new value across tabs
    } catch (error) {
        rollbackValue(); // rollback to the old value in case of failure
    }
};

You can also provide a new value to syncValue once the response is complete:

const handleOptimisticUpdate = async (newValue) => {
    const {rollbackValue, syncValue} = updateValueOptimistically(newValue);

    try {
        // Replace this with your actual API request or some operation that could fail
        const response = await fakeApiRequest();
        syncValue(response.data); // sync the new value across tabs
    } catch (error) {
        rollbackValue(); // rollback to the old value in case of failure
    }
};

Options

The useSyncedState hook accepts an optional Options object as its third parameter. The following options are available:

  • syncOnMount (boolean): Whether to sync the initial value on mount. Defaults to false.

Compatibility

This hook requires a browser environment that supports the BroadcastChannel API. Make sure to check its compatibility if you're targeting older browsers.

License

This project is open sourced under the MIT License.

Contribution

Contributions are welcome! Please open an issue if you find a bug or have a feature request. You can also propose changes via a pull request.

Author

Eddy Brown - GitHub