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-undo-redo-state

v1.10.6

Published

Effortlessly enable undo and redo capabilities in your React applications with this powerful custom hook. 'useUndoRedoState' empowers users with intuitive keyboard shortcuts and programmatic control, allowing them to revert or reapply state changes seamle

Downloads

75

Readme

useUndoRedoState

useUndoRedoState is a custom React hook that provides undo and redo functionality for state management. It allows you to easily integrate undo and redo capabilities into your React applications, giving users the ability to revert or reapply state changes with keyboard shortcuts or programmatic methods.

Features

  • Keyboard Shortcuts: The hook automatically sets up event listeners for keyboard shortcuts (Cmd+Z for undo and Cmd+Shift+Z for redo on macOS, or Ctrl+Z and Ctrl+Shift+Z on other platforms).
  • Programmatic Control: In addition to keyboard shortcuts, you can control undo and redo operations programmatically by calling the provided undo and redo functions.
  • Customizable Stack Size: You can customize the maximum size of the undo and redo stacks to control memory usage.
  • Callback Functions: The hook supports optional callback functions that are called after undo or redo operations, allowing you to perform additional actions or side effects.
  • Reset State: Provides a function to reset the state to its initial value.
  • Clear History: Provides a function to clear both the undo and redo stacks.

Installation

You can install the package via npm or yarn:

npm install react-undo-redo-state
yarn add react-undo-redo-state

Usage

import React from 'react';
import useUndoRedoState from 'react-undo-redo-state';

export default function CounterApp() {
  const [
    count,
    setCount,
    undo,
    redo,
    resetState,
    clearHistory
  ] = useUndoRedoState(0);

  return (
    <div className="w-full h-screen mt-20">
      <div className="flex flex-col gap-5 items-center">
        <p className="text-2xl">{count}</p>
        <div className="flex items-center gap-5">
          <button onClick={() => setCount(count - 1)}>Decrement</button>
          <button onClick={() => setCount(count + 1)}>Increment</button>
          <button onClick={undo}>Undo</button>
          <button onClick={redo}>Redo</button>
          <button onClick={resetState}>Reset</button>
          <button onClick={clearHistory}>Clear History</button>
        </div>
      </div>
    </div>
  );
}

The useUndoRedoState hook accepts two arguments:

  1. initialState: The initial state value.
  2. options (optional): An object containing configuration options:
    1. maxStackSize: The maximum size of the undo and redo stacks (default: 10).
    1. onUndo: A callback function to be called after an undo operation.
    1. onRedo: A callback function to be called after a redo operation.

The hook returns an array with six elements:

  1. state: The current state value.
  2. setValue: A function to set a new state value. This function automatically adds the previous state to the undo stack, enabling undo operations.
  3. undo: A function to revert the state to the previous value in the undo stack.
  4. redo: A function to apply the next value from the redo stack to the state.
  5. resetState: A function to reset the state to the initial value.
  6. clearHistory: A function to clear both the undo and redo stacks.

Demo

Check out the useUndoRedoState hook demo here featuring a simple Next.js counter app.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests on the GitHub repository

License

This project is licensed under the MIT License.

Summary of Changes (v1.10.6):

  1. Updated Features Section: Added details for the new resetState and clearHistory functions.
  2. Usage Example: Updated the usage example to show the use of the new functions and the renamed setValue function.
  3. Return Object Documentation: Updated the documentation to reflect returning an object instead of a tuple.