@friendofsvelte/state

中文文档
0.2.0发布于 2026/02/10MITTypeScriptES Module零依赖近 30 天下载 3586

Persistent Svelte 5 State, localStorage & sessionStorage

原始使用文档
文档目录9 节
  1. Features
  2. Installation
  3. Quick Start
  4. API Reference
  5. PersistentState<T>(key: string, initial?: T, storageType: S…
  6. Examples
  7. Basic Usage
  8. Contributing
  9. License

Persistent Svelte 5 State

A lightweight, type-safe state management solution for Svelte applications with built-in storage persistence.

Features

  • 🎯 Type-safe: Full TypeScript support with automatic type inference
  • 💾 Persistent Storage: Automatic state persistence in localStorage or sessionStorage
  • 🪶 Lightweight: Zero dependencies beyond Svelte
  • ⚡ Reactive: Seamless integration with Svelte's reactivity system
  • 🔄 Auto-sync: Automatically syncs state across components
  • 📦 Simple API: Just one function to manage all your state needs

Installation

npm install @friendofsvelte/state

Quick Start

  1. Define your state using PersistentState:
// new.svelte.ts / js
import { PersistentState } from '@friendofsvelte/state';

export const box = new PersistentState(
    'box',
    {
        color: '#ff3e00',
        dimensions: [100, 100]
    },
    'sessionStorage'
);
  1. Use in your components:
<script lang="ts">
    import { box } from '$lib/new.svelte';

    const listColors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];

    function switchNextColor() {
        const currentIndex = listColors.indexOf(box.current.color);
        const nextIndex = currentIndex + 1;
        if (nextIndex >= listColors.length) {
            box.current.color = listColors[0];
        } else {
            box.current.color = listColors[nextIndex];
        }
    }
</script>

<div
    style="background-color: {box.current
        .color}; width: 100px; height: 100px; color: gray; text-align: center;"
    class="m-2 rounded-2xl"
>
    {box.current.color}
</div>

<button onclick={switchNextColor} class="m-2 rounded-2xl bg-gray-700 px-3 text-gray-200">
    Change color
</button>

API Reference

PersistentState<T>(key: string, initial?: T, storageType: StorageType = 'localStorage')

Creates or retrieves a persistent state container.

Parameters:

  • key: Unique identifier for the state container
  • initial: (Optional) Initial state value
  • storageType: (Optional) Storage type - 'localStorage' or 'sessionStorage' (default: 'localStorage')

Returns:

  • A reactive state object of type T

Based on, Rich-Harris' local-storage-test

Examples

Basic Usage

<script lang="ts">
    import { PersistentState } from '@friendofsvelte/state';

    const box = new PersistentState(
        'box',
        {
            color: '#ff3e00',
            dimensions: [100, 100]
        },
        'sessionStorage'
    );

    function switchNextColor() {
        const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];
        const currentIndex = colors.indexOf(box.current.color);
        box.current.color = colors[(currentIndex + 1) % colors.length];
    }
</script>

<div
    style="background-color: {box.current
        .color}; width: 100px; height: 100px; color: gray; text-align: center;"
    class="m-2 rounded-2xl"
>
    {box.current.color}
</div>

<button onclick={switchNextColor} class="m-2 rounded-2xl bg-gray-700 px-3 text-gray-200">
    Change color
</button>

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details

版本与发布记录

@friendofsvelte/state 共发布 9 个版本,首次发布于 2025/02/15;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.2.0latest2026/02/109 个月前
0.0.6-ts2025/02/251.7 年前
0.0.62025/02/251.7 年前
0.0.52025/02/251.7 年前
0.0.42025/02/241.7 年前
0.0.32025/02/241.7 年前

关于 @friendofsvelte/state 的常见问题

@friendofsvelte/state 是什么?

Persistent Svelte 5 State, localStorage & sessionStorage

如何安装 @friendofsvelte/state?

在项目目录运行 npm install @friendofsvelte/state,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:svelte(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@friendofsvelte/state 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@friendofsvelte/state 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@friendofsvelte/state 可以商用吗?

@friendofsvelte/state 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@friendofsvelte/state 的下载量有多少?

根据 npm 官方下载统计,@friendofsvelte/state 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,586 次,日均约 120 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@friendofsvelte/state 还在维护吗?

最新版本 0.2.0 发布于 2026/02/10(9 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/02/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。