Skip to content

Commit 193835c

Browse files
dmaskaskydai-shi
andauthored
docs: add jotai-history website docs (#3025)
Co-authored-by: Daishi Kato <[email protected]>
1 parent 7ce754a commit 193835c

File tree

1 file changed

+58
-76
lines changed

1 file changed

+58
-76
lines changed

docs/third-party/history.mdx

+58-76
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,92 @@
11
---
22
title: History
3-
description: A Jōtai utility package for advanced state history management
4-
nav: 5.02
5-
keywords: history,undo,redo,jotai
3+
description: A Jōtai utility package for state history
4+
nav: 4.04
5+
keywords: history, undo, redo, track changes
66
---
77

8-
[jotai-history](https://github.com/jotaijs/jotai-history) is a utility package for advanced state history management.
8+
[jotai-history](https://github.com/jotaijs/jotai-history) is a utility package for tracking state history in Jotai.
99

10-
## install
10+
## Installation
1111

1212
```
1313
npm install jotai-history
1414
```
1515

16-
## withHistory
16+
## `withHistory`
1717

18-
### Signature
19-
20-
```ts
21-
declare function withHistory<T>(targetAtom: Atom<T>, limit: number): Atom<T[]>
22-
```
18+
```js
19+
import { withHistory } from 'jotai-history'
2320

24-
This function creates an atom that keeps a history of states for a given `targetAtom`. The `limit` parameter determines the maximum number of history states to keep.
25-
This is useful for tracking the changes over time.
21+
const targetAtom = atom(0)
22+
const limit = 2
23+
const historyAtom = withHistory(targetAtom, limit)
2624

27-
The history atom tracks changes to the `targetAtom` and maintains a list of previous states up to the specified `limit`. When the `targetAtom` changes, its new state is added to the history.
25+
function Component() {
26+
const [current, previous] = useAtomValue(historyAtom)
27+
...
28+
}
29+
```
2830

29-
### Usage
31+
### Description
3032

31-
```jsx
32-
import { atom, useAtomValue, useSetAtom } from 'jotai'
33-
import { withHistory } from 'jotai-history'
33+
`withHistory` creates an atom that tracks the history of states for a given `targetAtom`. The most recent `limit` states are retained.
3434

35-
const countAtom = atom(0)
36-
const countWithPrevious = withHistory(countAtom, 2)
35+
### Action Symbols
3736

38-
function CountComponent() {
39-
const [count, previousCount] = useAtomValue(countWithPrevious)
40-
const setCount = useSetAtom(countAtom)
37+
- **RESET**
38+
Clears the entire history, removing all previous states (including the undo/redo stack).
4139

42-
return (
43-
<>
44-
<p>Count: {count}</p>
45-
<p>Previous Count: {previousCount}</p>
46-
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
47-
</>
48-
)
49-
}
50-
```
40+
```js
41+
import { RESET } from 'jotai-history'
5142

52-
## withUndo
43+
...
5344

54-
### Signature
45+
function Component() {
46+
const setHistoryAtom = useSetAtom(historyAtom)
47+
...
48+
setHistoryAtom(RESET)
49+
}
50+
```
5551

56-
```ts
57-
type Undoable = {
58-
undo: () => void
59-
redo: () => void
60-
canUndo: boolean
61-
canRedo: boolean
62-
}
63-
declare function withUndo<T>(
64-
targetAtom: WritableAtom<T, [T], any>,
65-
limit: number,
66-
): Atom<Undoable>
67-
```
52+
- **UNDO** and **REDO**
53+
Moves the `targetAtom` backward or forward in its history.
6854

69-
`withHistory` provides undo and redo capabilities for an atom. It keeps track of the value history of `targetAtom` and provides methods to move back and forth through that history.
55+
```js
56+
import { REDO, UNDO } from 'jotai-history'
7057

71-
The returned object includes:
58+
...
7259

73-
- `undo`: A function to revert to the previous state.
74-
- `redo`: A function to advance to the next state.
75-
- `canUndo`: A boolean indicating if it's possible to undo.
76-
- `canRedo`: A boolean indicating if it's possible to redo.
60+
function Component() {
61+
const setHistoryAtom = useSetAtom(historyAtom)
62+
...
63+
setHistoryAtom(UNDO)
64+
setHistoryAtom(REDO)
65+
}
66+
```
7767

78-
### Usage
68+
### Indicators
7969

80-
```jsx
81-
import { atom, useAtom, useAtomValue } from 'jotai'
82-
import { withUndo } from 'jotai-history'
70+
- **canUndo** and **canRedo**
71+
Booleans indicating whether undo or redo actions are currently possible. These can be used to disable buttons or conditionally trigger actions.
8372

84-
const counterAtom = atom(0)
85-
const undoCounterAtom = withUndo(counterAtom, 5)
73+
```jsx
74+
...
8675

87-
function CounterComponent() {
88-
const { undo, redo, canUndo, canRedo } = useAtomValue(undoCounterAtom)
89-
const [value, setValue] = useAtom(counterAtom)
76+
function Component() {
77+
const history = useAtomValue(historyAtom)
9078

91-
return (
92-
<>
93-
<p>Count: {value}</p>
94-
<button onClick={() => setValue((c) => c + 1)}>Increment</button>
95-
<button onClick={undo} disabled={!canUndo}>
96-
Undo
97-
</button>
98-
<button onClick={redo} disabled={!canRedo}>
99-
Redo
100-
</button>
101-
</>
102-
)
103-
}
104-
```
79+
return (
80+
<>
81+
<button disabled={!history.canUndo}>Undo</button>
82+
<button disabled={!history.canRedo}>Redo</button>
83+
</>
84+
)
85+
}
86+
```
10587

10688
<CodeSandbox id="g6qj3q" />
10789

10890
## Memory Management
10991

110-
⚠️ Since `withHistory` and `withUndo` keeps a history of states, it's important to manage memory by setting a reasonable `limit`. Excessive history can lead to memory bloat, especially in applications with frequent state updates.
92+
> Because `withHistory` maintains a list of previous states, be mindful of memory usage by setting a reasonable `limit`. Applications that update state frequently can grow significantly in memory usage.

0 commit comments

Comments
 (0)