createTransaction

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

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

import { atom } from "valdres"
import { createTransaction } from "valdres-solid"

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

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

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

See also