4.11.2 • Published 3 years ago

@rooks/use-undo-state v4.11.2

Weekly downloads
118
License
MIT
Repository
github
Last release
3 years ago

@rooks/use-undo-state

Note: Future updates to this package have moved to the main package rooks. All hooks now reside in a single package which you can install using

npm install rooks

or

yarn add rooks

Rooks is completely treeshakeable and if you use only 1 of the 50+ hooks in the package, only that hook will be bundled with your code. Your bundle will only contain the hooks that you need. Cheers!

TitleCard

Build Status npm.io npm.io npm.io npm.io

About

Drop in replacement for useState hook but with undo functionality.

Installation

npm install --save @rooks/use-undo-state

Importing the hook

import useUndoState from '@rooks/use-undo-state'

Usage

const Demo = () => {
  const [value, setValue, undo] = useUndoState(0)

  return (
    <div>
      <span>Current value: {value}</span>
      <button onClick={() => setValue(value + 1)}>
        Increment
      </button>
      <button onClick={undo}>
        Undo
      </button>
    </div>
  )
}

render(<Demo/>)

You can pass any kind of value to hook just like React's own useState.

const Demo = () => {
  const [value, setValue, undo] = useUndoState({ data: 42 })

  return (
    <div>
      <span>Current value: {value}</span>
      <button onClick={() => setValue({ data: value.data + 1 })}>
        Increment object data
      </button>
      <button onClick={undo}>
        Undo
      </button>
    </div>
  )
}

render(<Demo/>)

Setter function can also be used with callback just like React's own useState.

const [value, setValue, undo] = useUndoState({ data: 42 })

() => setValue(current => current + 1)
const Demo = () => {
  const [value, setValue, undo] = useUndoState(0)

  return (
    <div>
      <span>Current value: {value}</span>
      <button onClick={() => setValue(current => current + 1)}>
        Increment
      </button>
      <button onClick={undo}>
        Undo
      </button>
    </div>
  )
}

render(<Demo/>)

To preserve memory you can limit number of steps that will be preserved in undo history.

const [value, setValue, undo] = useUndoState(0, { maxSize: 30 })

// now when calling undo only last 30 changes to the value will be preserved

Arguments

ArgumentsTypeDescriptionDefault value
initialValuebooleanInitial value of the statefalse
OptionsObjectAn options object for the hook{maxSize: 100}

Note: The second argument is an options object which currently accepts a maxSize which governs the maximum number of previous states to keep track of.

Returned array items

Returned Array itemsTypeDescription
valueAnyCurrent value
setValuefunctionSetter function to update value
undofunctionUndo state value
4.11.1

3 years ago

4.11.2

3 years ago

4.11.0

3 years ago

4.10.1

3 years ago

4.10.0

3 years ago

4.9.2

3 years ago

4.9.1

3 years ago

4.9.0

3 years ago

4.9.0-canary.0

3 years ago

4.8.1

3 years ago

4.8.0

3 years ago

4.7.1

3 years ago

4.7.0

3 years ago

4.6.1

3 years ago

4.6.0

3 years ago

4.5.0-canary.2

3 years ago

4.5.0

3 years ago

4.4.0

3 years ago

4.2.1-canary.0

3 years ago

4.2.1-canary.2

3 years ago

4.2.1-canary.3

3 years ago

4.3.0

3 years ago

4.2.0

3 years ago

4.1.1-canary.0

3 years ago

4.1.0

3 years ago

4.1.1

3 years ago

4.1.0-canary.2

3 years ago

4.1.0-canary.1

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0-canary.20

3 years ago

4.0.0-canary.23

3 years ago

4.0.0-canary.22

3 years ago

4.0.0

3 years ago

4.0.0-canary.17

3 years ago

4.0.0-canary.19

3 years ago

4.0.0-canary.16

3 years ago

4.0.0-canary.14

3 years ago

4.0.0-canary.13

3 years ago

4.0.0-canary.10

3 years ago

4.0.0-canary.9

3 years ago

4.0.0-canary.8

3 years ago

4.0.0-canary.6

4 years ago

4.0.0-canary.0

4 years ago