When writing incrementally-computed signals it is convenient (and usually more performant) to incrementally compute the diff too.

You can use this function to wrap the return value of a computed signal function to indicate that the diff should be used instead of calculating a new one with AtomOptions.computeDiff.

function withDiff<Value, Diff>(value: Value, diff: Diff): WithDiff<Value, Diff>

Example

const count = atom('count', 0)
const double = computed(
  'double',
  (prevValue) => {
    const nextValue = count.get() * 2
    if (isUninitialized(prevValue)) {
      return nextValue
    }
    return withDiff(nextValue, nextValue - prevValue)
  },
  { historyLength: 10 }
)

Parameters

NameDescription

value

Value

The value.

diff

Diff

The diff.

Returns

WithDiff<Value, Diff>
Prev
whyAmIRunning
Next
track

We use cookies on this website.
Learn more in our Cookie Policy.