Skip to content

Commit

Permalink
docs(readme): update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Mar 18, 2024
1 parent 3630e1e commit feed936
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
57 changes: 46 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,36 @@ yarn add use-travel mutative
- High performance
- Mark function for custom immutability

### TODO
### API

- [ ] add `archive` functionality
### Options

### API
| Options | type | description | default |
| ---------------- | ------------- | ------------------------------------- | -------------------------------- |
| `maxHistory` | number | The maximum number of history to keep | 10 |
| `initialPatches` | TravelPatches | The initial patches | {patches: [],inversePatches: []} |

### Return

| Return | type | description |
| --------------------- | -------------------------- | ------------------------------------------------------------------ |
| `state` | T | The current state |
| `setState` | Dispatch<T> | The state setter, support mutation update or return immutable data |
| `controls.back` | () => void | Go back to the previous state |
| `controls.forward` | () => void | Go forward to the next state |
| `controls.reset` | () => void | Reset the state to the initial state |
| `controls.canUndo` | () => boolean | Check if can go back to the previous state |
| `controls.canRedo` | () => boolean | Check if can go forward to the next state |
| `controls.getHistory` | () => T[] | Get the history of the state |
| `controls.patches` | TravelPatches[] | Get the patches history of the state |
| `controls.position` | number | Get the current position of the state |
| `controls.go` | (position: number) => void | Go to the specific position of the state |

```jsx
```tsx
import { useTravel } from 'use-travel';

const App = () => {
const [state, setState, controls]} = useTravel(0, {
const [state, setState, controls] = useTravel(0, {
maxHistory: 10,
initialPatches: [],
});
Expand All @@ -42,8 +61,12 @@ const App = () => {
<div>{state}</div>
<button onClick={() => setState(state + 1)}>Increment</button>
<button onClick={() => setState(state - 1)}>Decrement</button>
<button onClick={controls.back} disabled={!controls.canUndo()}>Undo</button>
<button onClick={controls.forward} disabled={!controls.canRedo()}>Redo</button>
<button onClick={controls.back} disabled={!controls.canUndo()}>
Undo
</button>
<button onClick={controls.forward} disabled={!controls.canRedo()}>
Redo
</button>
<button onClick={controls.reset}>Reset</button>
{controls.getHistory().map((state, index) => (
<div key={index}>{state}</div>
Expand All @@ -52,10 +75,22 @@ const App = () => {
<div key={index}>{patch}</div>
))}
<div>{controls.position}</div>
<button onClick={() => {
controls.go(1);
}}>Go</button>
<button
onClick={() => {
controls.go(1);
}}
>
Go
</button>
</div>
);
}
};
```

### TODO

- [ ] add `archive` functionality

## License

`use-travel` is [MIT licensed](https://github.com/unadlib/use-travel/blob/main/LICENSE).
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type TravelPatches = {
type Options<A extends boolean, F extends boolean> = {
maxHistory?: number;
initialPatches?: TravelPatches;
autoArchive?: A;
} & MutativeOptions<true, F>;

type InitialValue<I extends any> = I extends (...args: any) => infer R ? R : I;
Expand Down

0 comments on commit feed936

Please sign in to comment.