From cb78582dd10928967c52eedee7475b82ee9cfdf6 Mon Sep 17 00:00:00 2001 From: Kyle James <94993984+kj-49@users.noreply.github.com> Date: Sat, 6 Dec 2025 08:55:09 -0700 Subject: [PATCH] docs: expand state sharing options in documentation Added a third option for sharing state between modules by wrapping state in a class. --- documentation/docs/02-runes/02-$state.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/documentation/docs/02-runes/02-$state.md b/documentation/docs/02-runes/02-$state.md index 6fbf3b88955b..ec147f928d1d 100644 --- a/documentation/docs/02-runes/02-$state.md +++ b/documentation/docs/02-runes/02-$state.md @@ -319,7 +319,7 @@ import { count } from './state.svelte.js'; console.log(typeof count); // 'object', not 'number' ``` -This leaves you with two options for sharing state between modules — either don't reassign it... +This leaves you with three options for sharing state between modules — either don't reassign it... ```js // This is allowed — since we're updating @@ -334,7 +334,7 @@ export function increment() { } ``` -...or don't directly export it: +...don't directly export it: ```js let count = $state(0); @@ -347,3 +347,17 @@ export function increment() { count += 1; } ``` + +...or wrap your state in an instance of a class: + +```js +class CountStore { + count = $state(0); +} + +export let countStore = new CountStore(); + +export function increment() { + countStore.count += 1; +} +```