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

@ivbrajkovic/observable-context

v1.0.21

Published

Simple reactive context state

Downloads

6

Readme

🌟 Observable-Context 🌟

Banner Image

Reactive Contexts in React, Made Effortless! Supercharge your React context with built-in reactivity using observable-context.

Build Status npm version License: MIT


🚀 Features

  • 🎣 Hooks Ready: Custom hooks tailored for your context.
  • 📦 Zero Boilerplate: Set up reactive contexts without the repetitive code.
  • 🚀 Efficient Renders: Components re-render only when the observed properties change.
  • 🔌 Plug & Play: Integrates seamlessly with any React project.
  • ⚙️ Fully Typed: javascript support out-of-the-box.

🔄 Batching Updates

One of the standout features of observable-context is its ability to batch updates. Instead of triggering a re-render for each individual state change, you can batch multiple updates together and apply them all at once. This not only reduces the number of renders but also ensures a smoother user experience, especially when dealing with rapid state changes.

How to Use:

Batching updates is as simple as wrapping your update logic within beginBatchUpdate and endBatchUpdate calls.

const { beginBatchUpdate, endBatchUpdate, proxy } = useYourContext();

beginBatchUpdate();
proxy.key1 = "new value 1";
proxy.key2 = "new value 2";
// ... other updates
endBatchUpdate();

Between the beginBatchUpdate and endBatchUpdate calls, all changes to the context are accumulated. Once endBatchUpdate is called, all accumulated changes are applied simultaneously, triggering a single re-render.


📚 Getting Started

Installation

Using npm:

npm install @ivbrajkovic/observable-context --save

Or using yarn:

yarn add @ivbrajkovic/observable-context

Basic Usage with React

Setting up the Observable Context:

import React from "react";
import { observableContextFactory } from "@ivbrajkovic/observable-context";

export const user = {
  name: "John",
  age: 30,
  email: "[email protected]",
};

// Create an observable user context
export const {
  ContextProvider: UserProvider,
  useObservableContext,
  useWatch,
  useWatchList,
  useWatchAll,
} = observableContextFactory<typeof User>();

Using the Provider:

function App() {
  return (
    // Initial prop can be function that return initial state
    <UserProvider initial={user}>
      <UpdateName />
      <UpdateUserDetails />
      <UpdateCompleteUser />
    </UserProvider>
  );
}

export default App;

Using the Hooks:

function UpdateName() {
  const { state, setState } = useWatch("name");

  return (
    <div>
      <h1>Hello, {name}!</h1>
      <button onClick={() => setName("Jane")}>Change Name to Jane</button>
    </div>
  );
}
function UpdateUserDetails() {
  const { state, setState } = useWatchList(["name", "email"]);

  return (
    <div>
      <h1>{state.name}</h1>
      <p>Email: {state.email}</p>
      <button
        onClick={() => setState({ name: "Doe", email: "[email protected]" })}
      >
        Update Details
      </button>
    </div>
  );
}
function UpdateCompleteUser() {
  const { state, setState } = useSubscribeAll();

  return (
    <div>
      <h1>{state.name}</h1>
      <p>Age: {state.age}</p>
      <p>Email: {state.email}</p>
      <button
        onClick={() =>
          setValues({ name: "Alice", age: 25, email: "[email protected]" })
        }
      >
        Update All Details
      </button>
    </div>
  );
}

📖 Documentation

For in-depth documentation, guides, and API details, check here.


🤝 Contributing

Enhancements and improvements are welcome! See our CONTRIBUTING.md for more details.


📃 License

Observable-Context is MIT licensed.


💌 Contact & Support

For feedback, questions, or support, get in touch: