Description
Describe the problem
Store is elegant
src/use-store.ts:
import { writable } from "svelte/store";
export let s = writable(0);
src/store.svelte:
<script lang="ts">
import { s } from "./use-store";
</script>
<button on:click={() => ($s += 1)}> {$s}</button>
Rune and rune document is not elegant!
First, someone tries rune:
src/use-rune.ts
export let r = $state(0);
src/rune.svelte:
<script lang="ts">
import { r } from "./use-rune";
</script>
<button on:click={() => (r = r + 1)}>{r}</button>
ERROR: Cannot assign to import
Then they try
export let r = $state(0);
change to ->
export let r = $state(0);
export function increment() {
r += 1;
}
ERROR: The $state
rune is only available inside .svelte
and .svelte.js/ts
files
Then
They read https://svelte.dev/docs/svelte/svelte-js-files and try renaming src/use-rune.ts
to src/use-rune.svelte.ts
, and change all the imports. Then:
ERROR:Cannot export state from a module if it is reassigned. Either export a function returning the state value or only mutate the state value's properties
https://svelte.dev/e/state_invalid_export
Then they read https://svelte.dev/e/state_invalid_export
The website content:
state_invalid_export
Cannot export state from a module if it is reassigned. Either export a function returning the state value or only mutate the state value's properties
They don't know what function returning means
So they try:
export let r = $state(0);
export let getR = () => r;
and:
<script lang="ts">
import { getR } from "./use-rune.svelte";
</script>
<button on:click={() => getR()+=1}>{getR()}</button>
ERROR: Assigning to rvalue https://svelte.dev/e/js_parse_error
Then they are confused and read all the documents
Then they find the document https://svelte.dev/docs/svelte/ $state#Passing-state-across-modules
export let r = $state({ value: 0 });
export function increment() {
r.value += 1;
}
It works, but they don't like to define increment
.
Final
They change the code to:
export let r = $state({ value: 0 });
Describe the proposed solution
I think it's better to create $stateValue, $stateValue(xxx) = $state({ value: xxx });
Importance
nice to have