Appearance
Getting Started
Welcome to Nexus State! This guide will help you get started with using Nexus State in your projects.
Installation
To install Nexus State, run the following command:
bash
npm install @nexus-state/coreFor React integration:
bash
npm install @nexus-state/reactFor Vue integration:
bash
npm install @nexus-state/vueFor Svelte integration:
bash
npm install @nexus-state/svelteAdditional Packages
For async operations:
bash
npm install @nexus-state/asyncFor state families:
bash
npm install @nexus-state/familyFor Immer integration:
bash
npm install @nexus-state/immerFor middleware:
bash
npm install @nexus-state/middlewareFor persistence:
bash
npm install @nexus-state/persistFor CLI tools:
bash
npm install -g @nexus-state/cliFor developer tools:
bash
npm install @nexus-state/devtoolsFor Web Worker integration:
bash
npm install @nexus-state/web-workerBasic Usage
Here's a simple example of how to use Nexus State:
javascript
import { atom, createEnhancedStore } from '@nexus-state/core';
// Create an atom
const countAtom = atom(0, 'count');
// Create an enhanced store with time travel and DevTools
const store = createEnhancedStore([], {
enableTimeTravel: true,
enableDevTools: true
});
// Get the value of the atom
console.log(store.get(countAtom)); // 0
// Update the value of the atom
store.set(countAtom, 1);
// Functional update
store.set(countAtom, (prev) => prev + 1);
console.log(store.get(countAtom)); // 2
// Time travel
store.undo();
console.log(store.get(countAtom)); // 1
// Subscribe to changes
const unsubscribe = store.subscribe(countAtom, (value) => {
console.log('Count changed:', value);
});Package-Specific Usage
Async Operations
javascript
import { createAsyncOperation } from '@nexus-state/async';
const fetchData = createAsyncOperation(async () => {
const response = await fetch('/api/data');
return await response.json();
});
// Execute async operation
const data = await fetchData.execute();State Families
javascript
import { createFamily } from '@nexus-state/family';
const userFamily = createFamily({
profile: { name: '', email: '' },
preferences: { theme: 'light' }
});
// Access nested state
const name = userFamily.get('profile.name');Immer Integration
javascript
import { createImmerStore } from '@nexus-state/immer';
const store = createImmerStore({ users: [] });
// Update state with mutable API
store.setState((draft) => {
draft.users.push({ id: 1, name: 'John' });
});Middleware
javascript
import { createMiddleware } from '@nexus-state/middleware';
const logger = createMiddleware((action, next, store) => {
console.log('Action:', action);
return next(action);
});
store.use(logger);Persistence
javascript
import { createPersist } from '@nexus-state/persist';
const persist = createPersist({
key: 'app-state',
storage: localStorage
});
store.use(persist);Next Steps
- Learn about Core Concepts
- Explore the API reference
- Check out examples
- Try recipes
- See package-specific examples
- Migrating from v0.x? See the Migration Guide
Quick Links
- Installation - Detailed installation instructions
- Core Concepts - Understand the fundamental concepts
- Time Travel - Learn about Time Travel functionality
- DevTools - Debug with DevTools
- Best Practices - Follow best practices
- Performance Guide - Optimize performance