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

rx-global

v1.0.3

Published

Global state management for React. Global key-value approach. Set, use, get, update, subscribe, reset.

Downloads

8

Readme

RX-Global

Manage and interact with state from everywhere in your application.

This state management library is based on React.useState hook.

Minimal Setup and intuitive developer experience, esspecially with TypeScript.

Reference

npm i --save rx-global

// All exportet methods:
import {
	setGlobalState,
	useGlobalState,
	getGlobalState,
	updateGlobalState,
	subGlobalState,
	unsubGlobalState,
	resetGlobalState,
	resetGlobalStates
} from  'rx-global';

Best practive: keep id for state consistent.
// When named export, TypeScript will detects your state variables.
// This will improve developer experience.

export const COUNTER = "COUNTER";

// COUNTER will be used in snippets below!

setGlobalState(id: string, value: any):
// This will initialize the state for "COUNTER" to 0, if not existing.
// Otherwise it will set it to 0.
setGlobalState(COUNTER, 0);

Use cases:

  • Set initial value for a certain state (if not initialized, subGlobalState and updateGlobalState wont work)
  • Set a certain state from inside or outside of a React component, without knowing the previous state.

useGlobalState(id: string, initialValue?: any):
// React Hooks rules apply!
// InitialValue will initialize COUNTER to 0 if it hasnt
// been initialized otherwise. InitialValue is optional!
const [state, setState] = useGlobalState(COUNTER, 0);

// Same as useState but global
// const [state, setState] = useState(0);

Use Cases:

  • Use inside a React functional component to set and react to a certain state
  • Use useState untill a state needs to be shared globally and switch to useGlobalState.

getGlobalState(id: string):
// Get a certain global state
const counter = getGlobalState(COUNTER);

Use Cases:

  • Read state from anywhere without interacting with it.
  • Check for state in scripts outside of your components.

updateGlobalState(id: string, cb: Function => any):
// Update a certain state from anywhere.
// The callback will always give you access to the previous state
updateGlobalState(COUNTER, (state) => {
	if (state <= 0) {
		return 0;
	}
	return state - 1;
});

Use Cases:

  • Can be used anywhere you use setGlobalState
  • Update a certain state when you need to know the previous state
  • Example above: Prevent a counter from dropping below 0.

subGlobalState(id: string, cb: Function => void):
// Update a certain state from anywhere.
// The callback will always give you access to the previous state
const userFeedBack = (state) => {
	if (state === 10) {
		congratulateUser("You have reached 10 points");
	}
};
subGlobalState(COUNTER, userFeedBack);

Use Cases:

  • Use this method to have certain events be triggered based on a certain state change
  • Any kind of state validation
  • User feedback on certain state changes

! Since subGlobalState is not called from the scope of a React functional component, it is not using React.useState and cant unsubscribe based on component life-cycle. In order to safely use subGlobalState, use unsubGlobalState to clean up subscriptions when needed, to prevent a memory leak!

unsubGlobalState(id: string, cb: Function => void):
// This removes a subscribed callback function from a certain state.
unsubGlobalState(COUNTER, userFeedBack);

Use Cases:

  • As described in subGlobalState: clean up subscriptions when needed
  • Use together with subGlobalState to dynamically have states and components interact with each other. You can use other rx-global methods inside the subscription to have full control over your application state.

resetGlobalState(id: string):
// This resets a certain state back to its initial value.
resetGlobalState(COUNTER);

Use Cases:

  • Whenever you like to have the initial value again :)

resetGlobalStates(ids: string[]):
// In case you want to reset multiple states to its initial value.
resetGlobalState([COUNTER]);

Use Cases:

  • Whenever resetGlobalState is to slow.

Example

This is not an best practive example on how to use this library. Its a simplyfied display of what is possible! Please request more documentation for best practices and use cases!

import React from "react";
import {
	setGlobalState,
	useGlobalState,
	getGlobalState,
	updateGlobalState,
	subGlobalState,
	unsubGlobalState,
	resetGlobalState,
	resetGlobalStates
} from  'rx-global';

const COUNTER = "COUNTER";
const USER_WON = "USER_WON";

setGlobalState(COUNTER, 0);
setGlobalState(USER_WON); // initial value will be set in component

const checkPlayerWon = counter => {
	if (counter >= 10) {
		setGlobalState(USER_WON, true);
	}
};
const congratUser = userWon => {
	if (userWon) {
		const points = getGlobalState(COUNTER);
		alert(`You have won with ${points} points!`);
	}
};

subGlobalState(COUNTER, checkPlayerWon);
subGlobalState(USER_WON, congratUser);


const resetGame = () => {
	resetGlobalStates([COUNTER, USER_WON]);
};

export const Component = () => {
	const [counter, setCounter] = useGlobalState(COUNTER);
	const [userWon, setUserWon] = useGlobalState(USER_WON, false);

	useEffect(() => {
		return () => {
			unsubGlobalState(COUNTER, checkPlayerWon);
			unsubGlobalState(USER_WON, congratUser);
		}
	},[]);

	return (
		<div>
			{!userWon ? (
				<>
					<span>{`Count is: ${counter}`}</span>
					<button onClick={() => setCounter(counter + 1)}>
						Inc
					</button>
					<button onClick={() => setGlobalState(COUNTER, counter - 1)}>
						Dec
					</button>
					<button onClick={() => updateGlobalState(COUNTER, state => state * state)}>
						Sqr
					</button>
					<button onClick={() => resetGlobalState(COUNTER)}>
						Reset
					</button>
				</>
			): (
				<>
					<h4>Congrats you have won!</h4>
					<button onClick={resetGame}>Restart Game</button>
				</>
			)}
		</div>
	);
};

License MIT