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

xp.js-state

v0.2.0

Published

Global state management library for Modern React applications.

Downloads

4

Readme

xp.js(alpha) is a framework for Cross Platform React applications.

xp.js-state is an single package from that framework.

The goal of this package is to provide a simple declarative API to build complex yet scalable nested states that align with modern challenges, while ensuring a smooth developer experience, as well for your users.

Key features

  • Optimistic updates: Encourages developers to write optimistic updates by default.
  • High performance: Utilizes a signal-based architecture for efficient, fast updates to components.
  • Centralized state: Provides a predictable global state through mutations.
  • Data synchronization: With a mutation-change tracking system, you can subscribe to events, to easily synchorize, render optimistic mutations or rollback them.
  • Hook-Ready syntax: Easily integrates to React components using hooks yntax.
  • Type-Safe documentation: Offers type autocompletion and comprehensive documentation.

Quick Start

Installation

To get ready fast, here you can copy/paste to install from yarn or npm.

yarn add xp.js-state
npm install xp.js-state

Creating an observable Store

To begin, call the createStoreHook function to generate a new Hook.

Start with an initial state and a group of mutations. The mutations schema will provide a callback with a set of actions that can be performed.

type Book = {
  id: string;
  title: string;
  author: string;
  genre: string;
  publicationYear: number;
};

type AddBookParams = Omit<Book, 'id'>;
type RemoveBookParams = Pick<Book, 'id'>;

export const useBooks = createStoreHook({
  initialState: {
    books: [
      {
        id: '1fe8d8aa-ac7d-4bd3-b123-5ca0bea9fac4',
        title: "War and Peace",
        author: "Leo Tolstoy",
        genre: "Historical Fiction",
        publicationYear: 1869
      },
      {
        id: '04b2014f-3d6d-46a9-b5a4-c9345d6e3406',
        title: "Crime and Punishment",
        author: "Fyodor Dostoevsky",
        genre: "Psychological Fiction",
        publicationYear: 1866
      },
      {
        id: '46335384-9fab-41c9-abc0-b1b80e12fab6',
        title: "1984",
        author: "George Orwell",
        genre: "Dystopian Fiction",
        publicationYear: 1949
      },
    ],
  },
  mutations: ({ get, set }) => ({
    addBook: (params: AddBookParams) => {
      set({
        path: "books",
        value: [...get().books, { id: uuid(), ...params }],
      });
    },
    removeBook: (params: RemoveBookParams) => {
      const books = get().books.filter((book) => book.id !== params.id);
      set({ path: "books", value: books });
    },
  }),
});

This hook will be accessible globally, containing an array with your store state and an object with mutations.

Using the Hook in Components

This component demonstrates how to use the useBooks hook to manage a list of books. The addBook and removeBook functions update the global state, and the changes are reflected in the UI.

import React from "react";
import { useBooks, AddBookParams, RemoveBookParams } from "./useBooks";

function Component() {
  const [{ books }, { addBook, removeBook }] = useBooks();

  return (
    <div>
      <h2>Books</h2>
      <ul>
        {books.map((book) => (
          <li key={book.id}>
            <strong>{book.title}</strong> by {book.author} ({book.publicationYear})
            <button onClick={() => removeBook({ id: book.id })}>Remove this Book</button>
          </li>
        ))}
      </ul>
      <button
        onClick={() =>
          addBook({
            title: "New Book",
            author: "New Author",
            genre: "New Genre",
            publicationYear: 2024,
          })
        }
      >
        Add Book
      </button>
    </div>
  );
}