redux undo/redo immutable
simple undo/redo functionality for redux state containers using immutable.js
Protip: Check out the todos-with-undo example or the redux-undo-boilerplate to quickly get started with redux-undo
.
Switching from 0.x to 1.0 (beta): Make sure to update your programs to the latest History API.
This README is about the new 1.0-beta branch of redux-undo, if you are using
or plan on using 0.6, check out the 0.6
branch
Note on 1.0.0-beta7
If you use Redux Undo in CommonJS environment, don’t forget to add .default
to your import.
- var ReduxUndo = require('redux-undo')+ var ReduxUndo = require('redux-undo').default
If your environment support es modules just go by:
;
We are also supporting UMD build:
var ReduxUndo = windowReduxUndodefault;
once again .default
is required.
Installation
npm install --save redux-undo@beta
API
;
Making your reducers undoable
redux-undo
is a reducer enhancer (higher-order reducer), it provides the undoable
function, which
takes an existing reducer and a configuration object and enhances your existing
reducer with undo functionality.
Note: If you were accessing state.counter
before, you have to access
state.counter.present
after wrapping your reducer with undoable
.
To install, firstly import redux-undo
:
// Redux utility functions;// redux-undo higher-order reducer;
Then, add undoable
to your reducer(s) like this:
A configuration can be passed like this:
History API
Wrapping your reducer with undoable
makes the state look like this:
past: ...pastStatesHere... present: ...currentStateHere... future: ...futureStatesHere...
Now you can get your current state like this: state.present
And you can access all past states (e.g. to show a history) like this: state.past
Undo/Redo Actions
Firstly, import the undo/redo action creators:
;
Then, you can use store.dispatch()
and the undo/redo action creators to
perform undo/redo operations on your state:
store // undo the last actionstore // redo the last action store // undo 2 stepsstore // redo 5 steps store // jump to requested index in the past[] arraystore // jump to requested index in the future[] array store // [beta only] Remove all items from past[] and future[] arrays
Configuration
A configuration object can be passed to undoable()
like this (values shown
are default values):
Note: If you want to use just the initTypes
functionality, but not import
the whole redux-undo library, use redux-recycle!
Initial State and History
You can use your redux store to set an initial history for your undoable reducers:
; const initialHistory = past: 0 1 2 3 present: 4 future: 5 6 7 const store = ;
Or just set the current state like you're used to with Redux. Redux-undo will create the history for you:
; const store = ; // will make the state look like this: past: present: foo: 'bar' future:
Filtering Actions
If you don't want to include every action in the undo/redo history, you can
add a filter
function to undoable
. redux-undo
provides you with the
includeAction
and excludeAction
helpers for basic filtering.
They should be imported like this:
;
Now you can use the helper functions:
// they even support Arrays:
Note: Since beta4
,
only actions resulting in a new state are recorded. This means the
(now deprecated) distinctState()
filter is auto-applied.
Custom filters
If you want to create your own filter, pass in a function with the signature
(action, currentState, previousHistory)
. For example:
// The entire `history` state is available to your filter, so you can make// decisions based on past or future states:
Combining Filters
You can also use our helper to combine filters.
{ return actionwouldLikeToBeInHistory} { return staterecording}
Ignoring Actions
When implementing a filter function, it only prevents the old state from being
stored in the history. filter
does not prevent the present state from being
updated.
If you want to ignore an action completely, as in, not even update the present state, you can make use of redux-ignore.
It can be used like this:
// or define your own function:
What is this magic? How does it work?
Have a read of the Implementing Undo History recipe in the Redux documents, which explains in detail how redux-undo works.
Gitter Chat / Support
If you have a question or just want to discuss something with other redux-undo users/maintainers, chat with the community on gitter.im/omnidan/redux-undo
License
MIT, see LICENSE.md
for more information.