create API
The create
function is the main API for creating Storken stores.
Syntax
create<T>(config: StorkenConfig<T>): [
useStorken: Hook,
getStore: Getter,
setStore: Setter,
sky: SkyInstance
]
Parameters
config
Configuration object for the store
initialValues
- Initial state valuesgetters
- Async getters for lazy loadingsetters
- Async setters with side effectsplugins
- Store plugins
Examples
Basic Usage
import { create } from 'storken';
const [useStorken] = create({
initialValues: {
count: 0,
name: 'John'
}
});
With TypeScript
interface AppState {
user: User | null;
theme: 'light' | 'dark';
isAuthenticated: boolean;
}
const [useStorken] = create<AppState>({
initialValues: {
user: null,
theme: 'light',
isAuthenticated: false
}
});
Complete Example
const [useStorken, get, set, sky] = create({
initialValues: {
todos: [],
filter: 'all'
},
getters: {
todos: async () => {
const res = await fetch('/api/todos');
return res.json();
}
},
setters: {
todos: async (storken, value) => {
await fetch('/api/todos', {
method: 'PUT',
body: JSON.stringify(value)
});
}
}
});
// Use in components
function TodoList() {
const [todos, setTodos] = useStorken('todos', []);
// ...
}
// Use outside components
const currentTodos = sky.get('todos');
sky.set('filter', 'completed');
⚠️ Important
Always destructure the return value of create
to get the hooks and utilities you need. The most common pattern is to only destructure useStorken
.