Atomic transaction support for Jotai state updates.
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
# npm
npm install jotai-transaction
# yarn
yarn add jotai-transaction
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);
};
}
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);
}
};
// ...
}
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 committedonRollback?
: Callback function to run when transaction is rolled backlabel?
: Optional label for the transaction (useful for debugging)
Returns: A Transaction object with the following properties:
id
: Unique identifier for the transactionstatus
: Current status ('pending', 'committed', or 'rolled-back')set(atom, value)
: Method to stage an update to an atomget(atom)
: Method to get the current or staged value of an atom
Applies all staged changes in the transaction to the store.
Discards all staged changes in the transaction.
React hook for creating transactions. Takes the same options as beginTransaction
.
React hook for monitoring transaction status.
Returns:
activeTransactions
: Array of IDs for active transactionsisTransactionActive
: Boolean indicating if any transactions are activeregisterTransaction(id)
: Method to register a transactionunregisterTransaction(id)
: Method to unregister a transaction
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);
Transactions work with custom Jotai stores:
import { createStore } from 'jotai';
import { beginTransaction, commitTransaction } from 'jotai-transaction';
const myStore = createStore();
const transaction = beginTransaction({ store: myStore });