All Projects β†’ ScaleupConsulting β†’ useSharedState

ScaleupConsulting / useSharedState

Licence: other
useSharedState is a simple hook that can be used to share state between multiple React components.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to useSharedState

eventrix
Open-source, Predictable, Scaling JavaScript library for state managing and centralizing application global state. State manage system for react apps.
Stars: ✭ 35 (+Infinity%)
Mutual labels:  hooks, state-management, state
react-cool-form
😎 πŸ“‹ React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+Infinity%)
Mutual labels:  hooks, state-management, state
Use Global Context
A new way to use β€œuseContext” better
Stars: ✭ 34 (+Infinity%)
Mutual labels:  hooks, state-management, state
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (+Infinity%)
Mutual labels:  hooks, state-management, state
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+Infinity%)
Mutual labels:  hooks, state-management, state
Pullstate
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
Stars: ✭ 683 (+Infinity%)
Mutual labels:  hooks, state-management, state
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (+Infinity%)
Mutual labels:  hooks, state-management, state
enform
Handle React forms with joy 🍿
Stars: ✭ 38 (+Infinity%)
Mutual labels:  state-management, state
vuse-rx
Vue 3 + rxjs = ❀
Stars: ✭ 52 (+Infinity%)
Mutual labels:  state-management, state
react-state
React-State - Superpowers for managing local and reusable state in React
Stars: ✭ 14 (+Infinity%)
Mutual labels:  state-management, state
XUI
XUI makes modular, testable architectures for SwiftUI apps a breeze!
Stars: ✭ 100 (+Infinity%)
Mutual labels:  state-management, state
react-globally
Gives you setGlobalState, so you can set state globally
Stars: ✭ 22 (+Infinity%)
Mutual labels:  state-management, state
Reusable
Reusable is a library for state management using React hooks
Stars: ✭ 207 (+Infinity%)
Mutual labels:  hooks, state-management
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+Infinity%)
Mutual labels:  state-management, state
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+Infinity%)
Mutual labels:  hooks, state
atomic-state
Atomic State is a state management library for React
Stars: ✭ 15 (+Infinity%)
Mutual labels:  state-management, state
Reto
Flexible and efficient React Store with hooks.
Stars: ✭ 194 (+Infinity%)
Mutual labels:  hooks, state-management
storken
🦩 Storken is a React State Manager. Simple as `useState`.
Stars: ✭ 22 (+Infinity%)
Mutual labels:  state-management, state
snap-state
State management in a snap πŸ‘Œ
Stars: ✭ 23 (+Infinity%)
Mutual labels:  state-management, state
ReduxSimple
Simple Stupid Redux Store using Reactive Extensions
Stars: ✭ 119 (+Infinity%)
Mutual labels:  state-management, state

About this package

npm-badge

useSharedState is a simple hook that can be used to share state between multiple React components.

It relies on EventTarget, and as such is only currently supported in browsers. There is a line of code to disable it for SSR environments.

Table of Contents

Installation

This package can be installed via npm, using :

npm i @scaleup-consulting/use-shared-state

Dependencies:

  • react >= v16.8.0

Usage

Creating a shared state

Creating a shared state hook is no different than creating a simple react hook:

//sharedStates.js
import { SharedStateTarget } from "@scaleup-consulting/use-shared-state";
...

//declare the shared state with an initial value
export const SHARED_COUNTER = new SharedStateTarget(0);

...

Manipulating the state

To use and manipulate the shared state function similar to react hook syntax:

const [state, setState] = SHARED_STATE_NAME.useSharedState();

Usage in counter example:

//CounterButtons.jsx
...
import {SHARED_COUNTER} from "./sharedStates"

...


export function CounterButtons() {
  //get shared state and it's setter
  const [count, setCount] = SHARED_COUNTER.useSharedState();

  //use setter function to change the state's value
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increase</button>
      <button onClick={() => setCount(count - 1)}>Decrease</button>
    </div>
  );
}

...

Using the state

You can also subscribe to state's updates without using the setter function with

const [state] = SHARED_STATE_NAME.useSharedState();

Usage in counter example:

//Counter.jsx
...
import {SHARED_COUNTER} from "./sharedStates"

...

export function Counter() {
    //use shared state without setter
    const [count] = SHARED_COUNTER.useSharedState();

    //display state
    return <span>{count}</span>;
}

...

Best Practices

  • To make the Shared State easier to debug, there should be only one component responsible for setting the state.

Examples

More examples can be found at useSharedState-examples repository.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].