Drop in replacement for useState hook but with undo functionality.
npm install @rooks/use-undo-state
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
    
About
Drop in replacement for useState hook but with undo functionality.
Installation
`
npm install --save @rooks/use-undo-state
`
Importing the hook
`javascript
import useUndoState from '@rooks/use-undo-state'
`
Usage
`jsx
const Demo = () => {
const [value, setValue, undo] = useUndoState(0)
return (
Current value: {value}
)
}
render( )
`
You can pass any kind of value to hook just like React's own useState.
`jsx
const Demo = () => {
const [value, setValue, undo] = useUndoState({ data: 42 })
return (
Current value: {value}
)
}
render( )
`
Setter function can also be used with callback just like React's own useState.
`javascript
const [value, setValue, undo] = useUndoState({ data: 42 })
() => setValue(current => current + 1)
`
`jsx
const Demo = () => {
const [value, setValue, undo] = useUndoState(0)
return (
Current value: {value}
)
}
render( )
`
To preserve memory you can limit number of steps that will be preserved in undo history.
`javascript
const [value, setValue, undo] = useUndoState(0, { maxSize: 30 })
// now when calling undo only last 30 changes to the value will be preserved
``