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

@chrfalch/react-observable-context

v0.3.1

Published

Observable context for React

Downloads

9

Readme

React Observable Context

React's context is good - but we can do better!

  • 🧭 Avoid re-renders of root components with contexts
  • 👁️ Use observer based patterns to subscribe to changes in context
  • 👯‍♂️ Provides the same API as React's createContext

React Observable Context is a replacement for React's createContext method allowing you to create contexts that are directly mutable with subscriber support.

Example

Let's dive right in with a simple example of how to use React Observable Context:

import React from 'react';
import {
  createContext,
  useObserver,
} from '@chrfalch/react-observable-context';

// Let's declare our context - let's use our implementation of createContext:
const MyContext = createContext({
  counter: 0,
});

// Create a component using the context's value
const CurrentCount = () => {
  const context = React.useContext(MyContext);
  const { counter } = useObserver(context, 'counter');
  return <div>{counter}</div>;
};

// Create the app component that also uses the context
const App = () => {
  // Remember that per React documentation you can use a context without a provider
  // but you will get the default value - which is a valid object in this example.
  return (
    <div>
      <CurrentCount />
      <button title="Inc" onClick={() => MyContext.counter++}>
    </div>
  );
};

createContext

createContext is a replacement for React's createContext method. It accepts the same arguments and returns the same type, but the resulting context object is observable through the useObserver hook.

💡 You don't need a setter function (setCounter or increment) to update the context object - you can simply update the value directly on the Context object.

useObserver

The hook useObserver observes changes in an observable object. It accepts two arguments - the observable object and a path to the value you want to observe.

To subscribe to changes in a specific slice of the context, you can pass a path to the value you want to observe:

const ctx = React.useContext(MyContext)!;
const {counter} = useObserver(ctx, 'counter');

💡 You cannot set values with the state returned from useObserver - it is read-only. To update the context object, you need to update the value directly on the context object.

You can also pass multiple values to the useObserver hook to observe more than one value:

const ctx = React.useContext(MyContext)!;
const {counter, isPaused} = useObserver(ctx, 'counter', 'isPaused');

💡 Tips: Properties in a nested context will often end up with keys containing dots. To destructure such a property you can use the following syntax:

const { 'my.nested.key': myNestedKey } = useObserver(ctx, 'my.nested.key');

useObservable

This hook lets you create a memoized observable object. It accepts an object as an argument and returns a memoized observable object.

This hook is typically used to create the context object with a specific value when using a Context Provider:

// Context value will be a memoized observable object
const contextValue = useObservable({ counter: 1 });
return <MyContext.Provider value={contextValue}>{children}</MyContext.Provider>;

💡 Tips: The initial value will only be read once - the hook does not update the observable if the initial value is changed.