Skip to content

Commit

Permalink
Update to version 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Sep 16, 2024
1 parent 814e485 commit f992645
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm i jotai-history
### Signature

```ts
function withHistory<T>(targetAtom: Atom<T>, limit: number): Atom<T[]>
declare function withHistory<T>(targetAtom: Atom<T>, limit: number): Atom<T[]>
```

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.
Expand All @@ -30,7 +30,7 @@ import { withHistory } from 'jotai-history'
const countAtom = atom(0)
const countWithPrevious = withHistory(countAtom, 2)
export function CountComponent() {
function CountComponent() {
const [count, previousCount] = useAtomValue(countWithPrevious)
const setCount = useSetAtom(countAtom)
Expand All @@ -55,7 +55,7 @@ type Undoable<T> = {
canUndo: boolean
canRedo: boolean
}
function withUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number): Atom<Undoable>
declare function withUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number): Atom<Undoable>
```

`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.
Expand All @@ -76,8 +76,13 @@ import { withUndo } from 'jotai-history'
const counterAtom = atom(0)
const undoCounterAtom = withUndo(counterAtom, 5)
export function CounterComponent() {
const { undo, redo, canUndo, canRedo } = useAtomValue(undoCounterAtom)
function CounterComponent() {
const {
undo,
redo,
canUndo,
canRedo,
} = useAtomValue(undoCounterAtom)
const [value, setValue] = useAtom(counterAtom)
return (
Expand All @@ -96,7 +101,7 @@ export function CounterComponent() {
```

## Example
https://codesandbox.io/p/sandbox/musing-orla-g6qj3q?file=%2Fsrc%2FApp.tsx%3A10%2C23
https://codesandbox.io/p/sandbox/musing-orla-g6qj3q

## Memory Management

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jotai-history",
"description": "👻⌛",
"version": "0.2.0",
"version": "0.2.1",
"author": "David Maskasky",
"repository": {
"type": "git",
Expand Down

0 comments on commit f992645

Please sign in to comment.