npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

jotai-transaction

v0.2.0

Published

👻 Transaction support for Jotai atom updates

Readme

jotai-transaction

Atomic transaction support for Jotai state updates.

Overview

jotai-transaction extends Jotai's capabilities to support transactional updates across multiple atoms. This allows you to:

  • Group multiple atom updates into a single atomic operation
  • Stage changes without affecting the UI until explicitly committed
  • Roll back changes if an error occurs or conditions aren't met
  • Handle complex state transitions where partial updates would leave an inconsistent state

Installation

# npm
npm install jotai-transaction

# yarn
yarn add jotai-transaction

Basic Usage

import { atom, useAtom } from 'jotai';
import { beginTransaction, commitTransaction, rollbackTransaction } from 'jotai-transaction';

const countAtom = atom(0);
const messageAtom = atom('hello');

function MyComponent() {
  const [count, setCount] = useAtom(countAtom);
  const [message, setMessage] = useAtom(messageAtom);

  const handleSubmit = () => {
    const transaction = beginTransaction();
    
    // Stage changes (these won't update the UI yet)
    transaction.set(countAtom, count + 1);
    transaction.set(messageAtom, `Updated ${count + 1} times`);
    
    commitTransaction(transaction);
  };
}

React Hook Support

import { atom, useAtom } from 'jotai';
import { useTransaction } from 'jotai-transaction';

const formAtom = atom({ name: '', email: '', age: 0 });
const formStatusAtom = atom('idle');

function FormComponent() {
  const [form, setForm] = useAtom(formAtom);
  const [status, setStatus] = useAtom(formStatusAtom);
  
  const transaction = useTransaction({
    onCommit: () => console.log('Transaction committed successfully'),
    onRollback: () => console.log('Transaction rolled back')
  });
  
  const handleSubmit = async (e) => {
    e.preventDefault();
    
    transaction.set(formStatusAtom, 'submitting');
    transaction.set(formAtom, { ...form, name: form.name.trim() });
    
    try {
      commitTransaction(transaction);
      
      await submitToApi(transaction.get(formAtom));
      
      const successTransaction = beginTransaction();
      successTransaction.set(formStatusAtom, 'success');
      commitTransaction(successTransaction);
    } catch (error) {
      rollbackTransaction(transaction);
    }
  };
  
  // ...
}

API Reference

beginTransaction(options?)

Creates a new transaction for staging atom updates.

Options:

  • store?: Custom Jotai store (uses default store if not provided)
  • onCommit?: Callback function to run when transaction is committed
  • onRollback?: Callback function to run when transaction is rolled back
  • label?: Optional label for the transaction (useful for debugging)

Returns: A Transaction object with the following properties:

  • id: Unique identifier for the transaction
  • status: Current status ('pending', 'committed', or 'rolled-back')
  • set(atom, value): Method to stage an update to an atom
  • get(atom): Method to get the current or staged value of an atom

commitTransaction(transaction)

Applies all staged changes in the transaction to the store.

rollbackTransaction(transaction)

Discards all staged changes in the transaction.

useTransaction(options?)

React hook for creating transactions. Takes the same options as beginTransaction.

useTransactionStatus()

React hook for monitoring transaction status.

Returns:

  • activeTransactions: Array of IDs for active transactions
  • isTransactionActive: Boolean indicating if any transactions are active
  • registerTransaction(id): Method to register a transaction
  • unregisterTransaction(id): Method to unregister a transaction

Advanced Usage

Working with Derived Atoms

Transactions correctly handle derived atoms by recalculating their values based on staged primitive atom updates:

import { atom } from 'jotai';
import { beginTransaction, commitTransaction } from 'jotai-transaction';

const countAtom = atom(0);
const doubleAtom = atom((get) => get(countAtom) * 2);

const transaction = beginTransaction();
transaction.set(countAtom, 5);

// The derived value is calculated correctly within the transaction
console.log(transaction.get(doubleAtom)); // Output: 10

// But the UI won't update until committed
commitTransaction(transaction);

Custom Stores

Transactions work with custom Jotai stores:

import { createStore } from 'jotai';
import { beginTransaction, commitTransaction } from 'jotai-transaction';

const myStore = createStore();
const transaction = beginTransaction({ store: myStore });