Skip to main content

update

Updates a value in an interaction object using a transformation function.

Import

You can import the entire package and access the function:


_10
import * as sdk from "@onflow/sdk"
_10
_10
sdk.update(key, fn)

Or import directly the specific function:


_10
import { update } from "@onflow/sdk"
_10
_10
update(key, fn)

Usage


_16
import { update, put, initInteraction } from "@onflow/sdk"
_16
_16
const interaction = initInteraction();
_16
_16
// Set initial value
_16
put("counter", 0)(interaction);
_16
_16
// Increment counter
_16
const increment = update("counter", (current) => (current || 0) + 1);
_16
increment(interaction); // counter becomes 1
_16
increment(interaction); // counter becomes 2
_16
_16
// Update array
_16
put("tags", ["flow", "blockchain"])(interaction);
_16
const addTag = update("tags", (tags) => [...(tags || []), "web3"]);
_16
addTag(interaction); // tags becomes ["flow", "blockchain", "web3"]

Parameters

key

  • Type: string
  • Description: The dot-notation key path to update

fn (optional)

  • Type: (v: T | T[], ...args: any[]) => T | T[]
  • Description: The transformation function to apply to the existing value

Returns

function


Rate this page