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

@jeremyling/react-use-undoable-state

v1.1.0

Published

A lightweight module to store historical state and allow undo/redo

Downloads

110

Readme

React useUndoableState Hook

A lightweight module to store historical state and allow undo/redo, including multiple steps backward or forward.

Installation

npm install --save @jeremyling/react-use-undoable-state

The following packages are peer dependencies and must be installed for this package to work.

react
lodash

Usage Example

Here's a code sandbox for how this hook is used: https://codesandbox.io/s/use-undoable-state-2spts

import React from "react";
import useUndoableState from "@jeremyling/react-use-undoable-state";

export default function Document() {
  const {
    state: doc,
    setState: setDoc,
    resetState: resetDoc,
    index: docStateIndex,
    lastIndex: docStateLastIndex,
    goBack: undoDoc,
    goForward: redoDoc,
  } = useUndoableState(
    { text: "The quick brown fox jumps over the lazy dog" }, // initial value
    500 // debounce timeout before states gets updated (optional - defaults to 500)
  );

  const canUndo = docStateIndex > 0;
  const canRedo = docStateIndex < docStateLastIndex;

  return ...
}

Concept

As with useState, useUndoableState accepts only 1 argument, the initial value. Behind the scenes, the hook uses two main variables to determine state - index (number) and states (array). states stores the historical values of the state while index determines current state by indicating the current position in the array. states is only written to after a debounced period of debouncePeriod (passed as second param of hook, defaults to 500).

You may navigate through historical states by using the goBack and goForward functions emitted by the hook. However, if you make a call to setState and index is not at the end of the states array, all states after index is erased and index will go back to the end of the states array.

The following table attempts to provide a more detailed explanation of the object returned by the hook:

| Prop | Type | Usage | Description | | ---------- | ------------------------- | ----------------- | --------------------------------------------------------------------- | | state | T: any | | Current state, initialised with argument passed | | setState | (value: T) => void | setState(value) | Sets state to value. All values after current index is erased | | resetState | (init: T) => void | resetState(value) | Deletes historical states and resets to value | | index | number | | The current index in the states array | | lastIndex | number | | The last index in the states array. To determine if can goForward | | goBack | (steps: number) => void | goBack(2) | Goes back the number of steps passed | | goForward | (steps: number) => void | goForward(3) | Goes forward the number of steps passed |