Lightweight State Management for Svelte
A lightweight, type-safe state management solution for Svelte 5 applications inspired by Elm's Model-View-Update (MVU) architecture.
- Centralized State - Application state is organized in one location
- Automatic Updates - State changes trigger UI updates
- Structured Actions - Actions and updates follow a consistent pattern within a single class
- Redux DevTools Integration - Debug with time-travel debugging and state inspection
- Lightweight - Uses Svelte's built-in reactivity without external dependencies
Updatelogic fits perfectly when your application state has grown beyond simple component-level management. If you need centralized state but want to avoid the complexity of Redux or similar libraries, updatelogic provides the ideal balance of power and simplicity.
npm install updatelogicDefine a state class and create logic methods that modify it:
// counter.svelte.ts
import { updatelogic } from "updatelogic";
class State {
count = $state(0);
doubled = $derived(this.count * 2);
}
export const state = new State();
export const logic = updatelogic(
{
name: "counter",
state,
},
{
increment() {
state.count++;
},
},
);Use the logic and state in your components:
<script lang="ts">
import { state, logic } from "./counter.svelte.ts";
</script>
<button onclick={logic.increment}>
clicks: {state.count}
</button>The updatelogic function takes two arguments:
-
Config object with:
state: Your state class instancename(optional): Name for Redux DevToolsexcludeInDevTools(optional): Array of method names to exclude from DevTools
-
Logic object with methods that modify state
const logic = updatelogic(
{
state,
excludeInDevTools: ["helperMethod"],
},
{
helperMethod() {
state.count++;
},
increment() {
this.helperMethod();
},
},
);Install the Redux DevTools browser extension to inspect your state. Updatelogic tracks every method call and logs state changes with visual indicators:
→Method started✓Method completed successfully❌Method failed with error
You can view state snapshots, step through history, and monitor all actions.