TinyBase logoTinyBase

setValue

The setValue method sets a single keyed Value in the Store.

setValue(
  valueId: Id,
  value: Value | MapValue,
): Store
TypeDescription
valueIdId

The Id of the Value in the Store.

valueValue | MapValue

The Value to be set, or a MapValue function to update it.

returnsStore

A reference to the Store.

This method will cause listeners to be called for any Value, or Id changes resulting from it.

If the Value is invalid (either because of its type, or because it does not match a ValuesSchema associated with the Store), will be ignored silently.

As well as string, number, or boolean Value types, this method can also take a MapValue function that takes the current Value as a parameter and maps it. This is useful if you want to efficiently increment a value without fetching it first, for example.

The method returns a reference to the Store so that subsequent operations can be chained in a fluent style.

Examples

This example sets a single Value.

const store = createStore().setValue('open', true);
console.log(store.getValues());
// -> {open: true}

This example sets the data of a single Value by mapping the existing Value.

const increment = (value) => value + 1;
const store = createStore().setValues({employees: 3});

store.setValue('employees', increment);
console.log(store.getValue('employees'));
// -> 4

This example attempts to set an invalid Value.

const store = createStore().setValues({employees: 3});

store.setValue('bug', []);
console.log(store.getValues());
// -> {employees: 3}

Since

v3.0.0