Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 72d493a

Browse filesBrowse files
PuruVJRich-Harris
andauthored
docs(blog): Introducing Runes - mention .svelte.js suffix (#11522)
* Update 2023-09-20-runes.md * More file names * update import * remove unnecessary .svelte.js * Note for runes * Apply suggestions from code review Co-authored-by: Rich Harris <rich.harris@vercel.com> * Fix language, remove NOTE: * Reword the note * , -> and * Update documentation/blog/2023-09-20-runes.md Co-authored-by: Rich Harris <rich.harris@vercel.com> --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent 8e4c778 commit 72d493a
Copy full SHA for 72d493a

File tree

Expand file treeCollapse file tree

1 file changed

+11
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+11
-1
lines changed

‎documentation/blog/2023-09-20-runes.md

Copy file name to clipboardExpand all lines: documentation/blog/2023-09-20-runes.md
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authorURL: /
88
In 2019, Svelte 3 turned JavaScript into a [reactive language](/blog/svelte-3-rethinking-reactivity). Svelte is a web UI framework that uses a compiler to turn declarative component code like this...
99

1010
```svelte
11+
<!--- file: App.svelte --->
1112
<script>
1213
let count = 0;
1314
@@ -54,6 +55,7 @@ Runes are symbols that influence the Svelte compiler. Whereas Svelte today uses
5455
For example, to declare a piece of reactive state, we can use the `$state` rune:
5556

5657
```diff
58+
<!--- file: App.svelte --->
5759
<script>
5860
- let count = 0;
5961
+ let count = $state(0);
@@ -77,6 +79,7 @@ Well, no. The reality is that as applications grow in complexity, figuring out w
7779
With runes, reactivity extends beyond the boundaries of your `.svelte` files. Suppose we wanted to encapsulate our counter logic in a way that could be reused between components. Today, you would use a [custom store](https://learn.svelte.dev/tutorial/custom-stores) in a `.js` or `.ts` file:
7880

7981
```js
82+
/// file: counter.js
8083
import { writable } from 'svelte/store';
8184

8285
export function createCounter() {
@@ -92,7 +95,9 @@ export function createCounter() {
9295
Because this implements the _store contract_ — the returned value has a `subscribe` method — we can reference the store value by prefixing the store name with `$`:
9396

9497
```diff
98+
<!--- file: App.svelte --->
9599
<script>
100+
/// file: App.svelte
96101
+ import { createCounter } from './counter.js';
97102
+
98103
+ const counter = createCounter();
@@ -115,6 +120,7 @@ This works, but it's pretty weird! We've found that the store API can get rather
115120
With runes, things get much simpler:
116121

117122
```diff
123+
/// file: counter.svelte.js
118124
-import { writable } from 'svelte/store';
119125

120126
export function createCounter() {
@@ -131,8 +137,10 @@ export function createCounter() {
131137
```
132138

133139
```diff
140+
<!--- file: App.svelte --->
134141
<script>
135-
import { createCounter } from './counter.js';
142+
- import { createCounter } from './counter.js';
143+
+ import { createCounter } from './counter.svelte.js';
136144

137145
const counter = createCounter();
138146
</script>
@@ -143,6 +151,8 @@ export function createCounter() {
143151
</button>
144152
```
145153

154+
> Outside `.svelte` components, runes can only be used in `.svelte.js` and `.svelte.ts` modules.
155+
146156
Note that we're using a [get property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) in the returned object, so that `counter.count` always refers to the current value rather than the value at the time the function was called.
147157

148158
## Runtime reactivity

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.