useTransaction

Signature
useTransaction(store?: Store): (callback: (txn: Transaction) => any) => any

valdres-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