# mobx-time-traveler

> Make mobx store time-traveling with undo &amp; redo easily

Latest version **0.1.4** (published 2022-06-02) · ISC license · 0 weekly downloads

## Install

```sh
npm install mobx-time-traveler
pnpm add mobx-time-traveler
yarn add mobx-time-traveler
bun add mobx-time-traveler
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2022-06-02 |
| First published | 2021-06-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 37.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | huqingyang |

## Links

- npm: https://www.npmjs.com/package/mobx-time-traveler
- Repository: https://github.com/Aedron/mobx-time-traveler
- Homepage: https://github.com/Aedron/mobx-time-traveler#readme
- Issues: https://github.com/Aedron/mobx-time-traveler/issues
- npm.io page: https://npm.io/package/mobx-time-traveler

## Dependencies (1)

- [mobx](https://npm.io/package/mobx.md) ^6.6.0

## Recent versions

- 0.1.4 (latest) — 2022-06-02
- 0.1.3 — 2022-06-02
- 0.1.2 — 2021-06-15
- 0.1.1 — 2021-06-09
- 0.1.0 — 2021-06-09

## README

# mobx-time-traveler
Make mobx store time-traveling with undo &amp; redo easily

## Installation
```bash
npm install --save mobx-time-traveler
```

## Usage
```tsx
import { observable } from 'mobx';
import { observer } from 'mobx-react';
import { withTimeTravel, actionWithSnapshot, timeTraveler } from 'mobx-time-traveler';

// Make this store's state can be collected and restored by time-traveler
// Multi stores are supported, but make sure that every store has an unique class name
@withTimeTravel
class MyStore {
    @observable
    foo = 1;

    // Mark this action's effects recorded by time-traveler
    // Can also use "@action @withSnapshot"
    @actionWithSnapshot
    setFoo = (foo: number) => {
        this.foo = 1;
    }
}

// Make sure every store will be instantiated only once
const myStore = new MyStore();

const App = observer(() => {
    // Using time-traveler's state in component with observer
    const { undo, redo, canUndo, canRedo } = timeTraveler;
    const { foo, setFoo } = myStore;
    
    const onChange = useCallback(() => setFoo(Math.random() * 100), []);

    useEffect(() => {
        // Record current state as initial state
        timeTraveler.initSnapshots();
    }, []);

    return (
        <div>
            <p>{foo}</p>
            <button onClick={onChange}>random change</button>
            {canUndo ? <button onClick={undo}>undo</button> : null}
            {canRedo ? <button onClick={redo}>redo</button> : null}
        </div>
    );
});
```

## API

* withTimeTravel
* withSnapshot
* actionWithSnapshot
* timeTraveler
* configure

---
_Source: https://npm.io/package/mobx-time-traveler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
