On this page
On this page
Move Logic from Components to Stores
Stores are not only to keep values. You can use them to track time, to load data from server.
import { atom, onMount } from 'nanostores'
export const $currentTime = atom<number>(Date.now())
onMount($currentTime, () => { $currentTime.set(Date.now()) const updating = setInterval(() => { $currentTime.set(Date.now()) }, 1000) return () => { clearInterval(updating) }})Use derived stores to create chains of reactive computations.
import { computed } from 'nanostores'
import { $currentTime } from './currentTime.js'
const appStarted = Date.now()
export const $userInApp = computed($currentTime, currentTime => { return currentTime - appStarted})We recommend moving all logic, which is not highly related to UI, to the stores. Let your stores track URL routing, validation, sending data to a server.
With application logic in the stores, it is much easier to write and run tests. It is also easy to change your UI framework. For instance, add React Native version of the application.
Imported from the Nano Stores README for version 1.5.5. Improve this page there.