Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More β†’
Socket
Book a DemoInstallSign in
Socket

jotai-history

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jotai-history

πŸ‘»βŒ›

latest
Source
npmnpm
Version
0.4.3
Version published
Weekly downloads
9.6K
-43.98%
Maintainers
2
Weekly downloads
Β 
Created
Source

jotai-history

jotai-history is a utility package for tracking state history in Jotai.

Installation

npm install jotai-history

withHistory

import { withHistory } from 'jotai-history'

const targetAtom = atom(0)
const limit = 2
const historyAtom = withHistory(targetAtom, limit)

function Component() {
  const [current, previous] = useAtomValue(historyAtom)
  ...
}

Description

withHistory creates an atom that tracks the history of states for a given targetAtom. The most recent limit states are retained.

Action Symbols

  • RESET
    Clears the entire history, removing all previous states (including the undo/redo stack).

    import { RESET } from 'jotai-history'
    
    ...
    
    function Component() {
      const setHistoryAtom = useSetAtom(historyAtom)
      ...
      setHistoryAtom(RESET)
    }
    
  • UNDO and REDO
    Moves the targetAtom backward or forward in its history.

    import { REDO, UNDO } from 'jotai-history'
    
    ...
    
    function Component() {
      const setHistoryAtom = useSetAtom(historyAtom)
      ...
      setHistoryAtom(UNDO)
      setHistoryAtom(REDO)
    }
    

Indicators

  • canUndo and canRedo
    Booleans indicating whether undo or redo actions are currently possible. These can be used to disable buttons or conditionally trigger actions.

    ...
    
    function Component() {
      const history = useAtomValue(historyAtom)
    
      return (
        <>
          <button disabled={!history.canUndo}>Undo</button>
          <button disabled={!history.canRedo}>Redo</button>
        </>
      )
    }
    

Memory Management

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.

Keywords

jotai

FAQs

Package last updated on 13 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts