Like transaction, but does not create a new transaction if there is already one in progress. This is the preferred way to batch state updates when you don't need the rollback functionality.

function transact<T>(fn: () => T): T

Example

const count = atom('count', 0)
const doubled = atom('doubled', 0)

react('update doubled', () => {
  console.log(`Count: ${count.get()}, Doubled: ${doubled.get()}`)
})

// This batches both updates into a single reaction
transact(() => {
  count.set(5)
  doubled.set(count.get() * 2)
})
// Logs: "Count: 5, Doubled: 10"

Parameters

NameDescription

fn

() => T

The function to run in a transaction

Returns

T

The return value of the function

Prev
reactor
Next
transaction