neuron
Home / vanilla / Read and Update State
Reading and updating state in Neuron is very easy. You must call the methods below to perform each task.
Calling the get
function will return the current value of the state. You must pass the correct key
as a parameter and the store will use this to grab the correct state.
const currentState = Store.get("name");
console.log(currentState);
//Ash Ketchum
To update state you must call the set
function. It takes two parameters, key
and newValue
.
Store.set("name", "Gary Oak");
//The new state would be 'Gary Oak'
The set
method can also take a predicate as its second parameter to grab the previous state value. Example below.
Store.set("name", (prev) => `${prev} is lame.`);
//The new state would be 'Gary Oak is lame'