Skip to content

Examples

Real-world examples of using the library.

Save and restore atom values using the browser’s local storage.

import { atom } from '@reaxium/core';
import { LocalStorageSystem } from '@reaxium/core';
const counter = atom(0, LocalStorageSystem('counter'));
counter.set(1);
// Value is automatically saved to localStorage

Create a computed value that depends on multiple atoms.

import { atom, computed } from '@reaxium/core';
const width = atom(10);
const height = atom(20);
const area = computed(
() => width.get() * height.get(),
[width, height]
);
console.log(area.get()); // 200
width.set(5);
console.log(area.get()); // 100

React to state changes with subscriptions.

import { atom } from '@reaxium/core';
const user = atom({ name: 'Alice' });
const unsubscribe = user.subscribe((value) => {
console.log('User changed:', value);
});
user.set({ name: 'Bob' });
// Logs: "User changed: { name: 'Bob' }"
unsubscribe(); // Stop listening