Skip to content

Commit 77646c9

Browse files
committed
add clarification on non reactive template expressions
1 parent b6bf735 commit 77646c9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

documentation/docs/03-template-syntax/01-basic-markup.md

+19
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ Curly braces can be included in a Svelte template by using their [HTML entity](h
158158

159159
If you're using a regular expression (`RegExp`) [literal notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor), you'll need to wrap it in parentheses.
160160

161+
> [!NOTE] Svelte statically analyzes your code to determine what is actually reactive. As a result, if a text or attribute expression does not contain:
162+
> - An object property
163+
> - A function call
164+
> - A reference to a reactive variable
165+
>
166+
> Then Svelte will treat it as a static expression that does not require updates. This means that this sort of statement would be treated as static, and would never update:
167+
> ```svelte
168+
> <script>
169+
> let count = $state(0);
170+
> let thing = {
171+
> toString() {
172+
> return count;
173+
> }
174+
> };
175+
> </script>
176+
> <button onclick={() => count++}>Count is {thing}</button>
177+
> ```
178+
> In the above case, if you wanted the statement to be reactive, you would have to append `.toString()` to the expression (to turn `{thing}` into `{thing.toString()}`).
179+
161180
<!-- prettier-ignore -->
162181
```svelte
163182
<h1>Hello {name}!</h1>

0 commit comments

Comments
 (0)