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

react-observable-state

v1.0.2

Published

Powerful state management in React

Downloads

9

Readme

react-observable-state

react-observable-state is a lightweight state management library for React applications. It uses the power of Observable pattern and React hooks to manage and update application state efficiently. The library provides an easy way to subscribe to state changes, and it updates only the components that are subscribed to specific parts of the state.

Installation

npm install react-observable-state

Usage

import { useObservable, Observable } from "react-observable-state";

Then, create a global state using the Observable function:


const State = Observable({
  counter: 0,
  user: {
    name: 'John Doe',
    email: '[email protected]',
  },
});

or


const State = Observable();
State.counter = 0,
State.user = {
    name: 'John Doe',
    email: '[email protected]',
  }

In your React components, you can use the useObservable hook to subscribe to state changes:


import React from 'react';
import { useObservable } from 'react-observable-state';

const Counter = () => {
  const state = useObservable('counter');
  const increment = () => state.update({ counter: state.counter + 1 });

  return (
    <div>
      <p>Counter: {state.counter}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
};

Here, we subscribe to changes in the counter property of the state. Whenever the counter value is updated, the Counter component will re-render.

You can subscribe to other states at the same time


window.State = Observable();

State.userFirst = {
  name: 'John Doe',
  email: '[email protected]',
}
State.userSecond = {
  name: 'Smith',
  email: 'not indicated',
};


const App = () => {

  const state = useObservable('userFirst, userSecond');

  const handleClick = () => {
    State.update({ userFirst: { email: 'not indicated' }, userSecond: { name: "Simon Smith" } });
  };

  return (
    <div>
      <p>{state.userFirst.email}</p>
      <p>{state.userSecond.name}</p>
      <button onClick={handleClick}>Change</button>
    </div>
  );
};

export default App;

Overall

This code exports two functions useObservable and Observable.

Observable function creates an object with the ability to notify subscribers of any changes to its properties. It does so by creating a Proxy object, which intercepts property accesses and updates, and sends notifications to all subscribed functions. The subscribe function adds a new subscriber to a particular property path. The update function updates the object with a new partial object.

useObservable is a custom React hook that takes in a comma-separated list of property paths and an optional timeout value. It creates a state variable with useState and a rerender function. Then it uses useEffect and the juxt function from Ramda to subscribe to the specified property paths and pass the rerender function to each subscription. Finally, it returns the observable object created by the Observable function.

The throttle and debounce functions from the throttle-debounce library are used to limit the number of times the component rerenders in response to changes in the observable object. The without, split, keys, and juxt functions from Ramda are used to manipulate arrays and objects in a functional programming style.

Overall, this code provides a way to manage state in a React application using an observable object with subscribers, allowing for reactive updates to the UI without the need for manual state management.

Good luck and do not forget 42 the answer to life the universe and everything.