This repository was archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Usage
Darko Kukovec edited this page Jun 24, 2017
·
1 revision
To add a value to the object, you can either pass it as initial value when creating the store or add it with the setItem method:
import KeysStore from 'mobx-keys-store';
const store = new KeysStore({a: 1});
store.setItem('b', 2);Since the store is using MobX, the same limitation applies - you should never set properties directly, or they won't be observable.
You can get the value directly from the object, or use the getItem method:
console.log(store.a); // 1
console.log(store.getItem('b')); // 2To up[date an existing value, you can either modify it directly or use setItem:
store.a = 3;
store.setItem('b', 4);The advantage of using setItem is that you don't need to worry if the value is already set - it will be observable in both cases.
To make working with numbers, there are two helper methods on the store - increaseItem and decreaseItem:
store.increaseItem('a'); // equivalent to store.a++
store.decreaseItem('b', 2); // equivalent to store.b -= 2;To delete an item, use the removeItem method:
store.removeItem('a');