Skip to content

Initialize State

Anil Maharjan edited this page Dec 16, 2020 · 6 revisions

Use .useState() method to initialize a state, useState() accepts 3 parameters. Returns getter and setter functions

/**
 *
 * @param {string} state name of the state
 * @param {any} initialVal initial value
 * @param {function} watcher watcher function that will be called everytime the value of the state changes
 *
 * @returns {Array<[function, function]>} getterFunction and SetterFunction
 */

Example:

const [count, setCount] = app.useState('count', 3, countWatcher);

In the above example count is a getter function which you can call to get the current state of count.

const currentCount = count();

Read more about ← How to initialize a Comfey Component | Comfey state Watchers →