useTransaction
Signature
useTransaction(store?: Store): (callback: (txn: Transaction) => any) => anyvaldres-vue Batch updates — subscribers fire once
Returns a function that runs a callback as a transaction on the current store: all updates apply atomically and subscribers fire once at the end. The callback receives { set, get, reset, del, unset }.
Usage
<script setup>
import { atom } from "valdres"
import { useTransaction } from "valdres-vue"
const firstNameAtom = atom("")
const lastNameAtom = atom("")
const txn = useTransaction()
const clear = () =>
txn(({ reset }) => {
reset(firstNameAtom)
reset(lastNameAtom)
})
</script>
<template>
<button @click="clear">Clear</button>
</template>
Pass a store to target one explicitly: useTransaction(myStore).
See also
- Transactions guide — semantics, nesting, and performance
- useStore — access the current store