# useTransaction

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

valdres-react
Batch updates — subscribers fire once

Returns a function that runs a callback as a [transaction](https://valdres.dev/guides/transactions) on the current store: all updates apply atomically and subscribers fire once at the end. The callback receives `{ set, get, reset, del, unset }`.

## Usage

```tsx
import { atom } from "valdres"
import { useTransaction } from "valdres-react"

const firstNameAtom = atom("")
const lastNameAtom = atom("")

function ResetForm() {
    const txn = useTransaction()
    const clear = () =>
        txn(({ reset }) => {
            reset(firstNameAtom)
            reset(lastNameAtom)
        })
    return <button onClick={clear}>Clear</button>
}
```

Pass a store to target one explicitly: `useTransaction(myStore)`.

## See also

- [Transactions guide](https://valdres.dev/guides/transactions) — semantics, nesting, and performance
- [useValdresCallback](https://valdres.dev/react/useValdresCallback) — memoized callback with transactional set/get
- [useStore](https://valdres.dev/react/useStore) — access the current store
