Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including react-undo with all npm packages installed. Try it out:

require("prop-types/package.json"); // prop-types is a peer dependency. var reactUndo = require("react-undo")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

react-undo v1.2.1

React Component for easy undo/redo on any component's props

React Undo

Tiny React Undo library with no dependencies!

Travis npm Coverage Status npm npm Codacy grade

Most undo/redo solutions use redux, which is amazing except when you need something a bit simpler.


$ npm i react-undo

Or if you prefer yarn

$ yarn add react-undo


    as={ YourComponent }
    props={ { propsForYourComponent: true } }
    trackProps={ ['propName'] }
    onChange={ this.doSomething }

It wraps your component (in the example above YourComponent) passing it the props that you define, and tracking any prop defined in trackProps. It doesn't support tracking nested props as of now (PR welcome :wink:)

If the child component calls redo() or undo(), the UndoRedo component will trigger the function defined in onChange, passing an object with all the keys defined in trackProps. You can then update your state/whatever so that the props that are passed to the child component are updated.


Docs on each prop, see them in action in the example below.


Component that you are wrapping.


Any props you were passing to your component.


Array of prop names that you want to track. These should be keys of the object props passed.


This will be fired when moving back and forth (undo/redo).

Props passed to Your Component

Your component will receive all the props you pass it, plus an object containing some useful methods:

// props received by your component
const props = {
  { ...props }, // all the ones passed by you
  undoRedo: {
    canUndo(): boolean,
    canRedo(): boolean,
    redo(): void,
    undo(): void,
    addStep(): void,

canUndo / canRedo

Methods returning true if there are elements to undo/redo in the history.

redo / undo

Move backwards or forwards in the history by one element. It will trigger onChange with the relevant history element.


Store the current value of the tracked properties in the history as a new step. It will do a quick === comparison with the previous values to try to avoid duplicates.


This example was taken from example/app/src/ExamplePage.js which you can see running at

import React from 'react';
import PropTypes from 'prop-types';

export default class Input extends React.PureComponent {

  constructor(props) {

    this.state = {
      isUndoRedo: false, // track updates
      currentValue: '',

  undo = (e) => {
      isUndoRedo: true,

  redo = (e) => {
      isUndoRedo: true,

  update = (e) => {
      currentValue: '',
    // addStep needs to be called once the new value is
    // in the undoRedo component. a setTimeout can achieve this.
    // In more complex use cases you need to determine the right moment and place for this.
    setTimeout(() => {

  render() {
    return (
      <div className='card'>
          Current value: <code>{ this.props.val }</code>
            value={ this.state.currentValue }
            onChange={ (e) => { this.setState({ currentValue: }); } }
            placeholder='Enter value...'
            onClick={ this.update }
            disabled={ this.state.currentValue === '' }
        <hr />
          <code>UndoRedo</code> Actions:
            disabled={ !this.props.undoRedo.canUndo() }
            onClick={ this.undo }
            disabled={ !this.props.undoRedo.canRedo() }
            onClick={ this.redo }

Input.propTypes = {
  undoRedo: PropTypes.object.isRequired, // provided by the UndoRedo wrapper
  val: PropTypes.string.isRequired,
  update: PropTypes.func.isRequired,


Only edit the files in the src folder. I'll update dist manually before publishing new versions to npm.

To run the tests simply run npm test. Add tests as you see fit to the test folder, they must be called {string}.test.js.


Copyright © Alejandro U. Alvarez 2017. MIT Licensed.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free