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

easy-history

v1.0.6

Published

Simple library to track, undo, and redo state changes.

Downloads

10

Readme

Easy History

Easy History is a lightweight, flexible state management solution for React applications that provides undo, redo, batch updates, and snapshotting functionality. It consists of two main components: a History class for managing state history, and a useHistory hook for easy integration with React components.

Features

  • Undo and redo functionality
  • Batch updates for grouping multiple state changes
  • Snapshotting for saving and restoring state
  • Customizable history size
  • Custom equality comparison for state updates

Usage

Here's an example of how to use the useHistory hook in a React component, showcasing the features:

import React from 'react';
import { useHistory } from 'easy-history';

function Counter() {
  const { 
    state, 
    set, 
    undo, 
    redo, 
    batch,
    takeSnapshot,
    restoreSnapshot,
    canUndo, 
    canRedo 
  } = useHistory({ count: 0, name: '' });

  const [savedSnapshot, setSavedSnapshot] = React.useState(null);

  return (
    <div>
      <p>Count: {state.count}</p>
      <p>Name: {state.name}</p>
      <button onClick={() => set({ ...state, count: state.count + 1 })}>Increment</button>
      <button onClick={() => set({ ...state, count: state.count - 1 })}>Decrement</button>
      <button onClick={() => batch(current => ({
        ...current,
        count: current.count + 1,
        name: 'Updated'
      }))}>Batch Update</button>
      <button onClick={() => {
        const snapshot = takeSnapshot();
        setSavedSnapshot(snapshot);
      }}>Save Snapshot</button>
      <button onClick={() => {
        if (savedSnapshot) {
          restoreSnapshot(savedSnapshot);
        }
      }} disabled={!savedSnapshot}>Restore Snapshot</button>
      <button onClick={undo} disabled={!canUndo}>Undo</button>
      <button onClick={redo} disabled={!canRedo}>Redo</button>
    </div>
  );
}

API

useHistory(initialState, options?)

A React hook that provides history management for your state.

Parameters:

  • initialState: The initial state of your data.
  • options (optional): An object with the following optional properties:
    • maxSize: The maximum number of past states to keep in history (default: Infinity).
    • isEqual: A function to determine if two states are equal (default: strict equality).

Returns:

An object with the following properties:

  • state: The current state.
  • set(newState): Function to update the state.
  • undo(): Function to undo the last change.
  • redo(): Function to redo the last undone change.
  • batch(updateFn): Function to perform multiple updates as a single history entry.
  • takeSnapshot(): Function to save the current state of the history.
  • restoreSnapshot(snapshot): Function to restore a previously saved history state.
  • canUndo: Boolean indicating if undo is possible.
  • canRedo: Boolean indicating if redo is possible.

Advanced Usage

Batch Updates

Use the batch function to group multiple state updates into a single history entry:

batch((currentState) => ({
  ...currentState,
  count: currentState.count + 1,
  name: 'New Name'
}));

Snapshotting

Save and restore the entire state of the history:

// Save a snapshot
const snapshot = takeSnapshot();

// Later, restore the snapshot
restoreSnapshot(snapshot);

Custom Equality Comparison

Provide a custom equality function to control when new history entries are created:

const { state, set } = useHistory(
  { count: 0, name: '' },
  { 
    // Only create new history when count changes
    isEqual: (a, b) => a.count === b.count 
  }
);