Skip to content

Commit 7f526a4

Browse files
Sync svelte docs (#1622)
sync svelte docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com>
1 parent 4cd47dd commit 7f526a4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

apps/svelte.dev/content/docs/svelte/02-runes/02-$state.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snaps
167167

168168
This is handy when you want to pass some state to an external library or API that doesn't expect a proxy, such as `structuredClone`.
169169

170+
## `$state.eager`
171+
172+
When state changes, it may not be reflected in the UI immediately if it is used by an `await` expression, because [updates are synchronized](await-expressions#Synchronized-updates).
173+
174+
In some cases, you may want to update the UI as soon as the state changes. For example, you might want to update a navigation bar when the user clicks on a link, so that they get visual feedback while waiting for the new page to load. To do this, use `$state.eager(value)`:
175+
176+
```svelte
177+
<nav>
178+
<a href="/" aria-current={$state.eager(pathname) === '/' ? 'page' : null}>home</a>
179+
<a href="/about" aria-current={$state.eager(pathname) === '/about' ? 'page' : null}>about</a>
180+
</nav>
181+
```
182+
183+
Use this feature sparingly, and only to provide feedback in response to user action — in general, allowing Svelte to coordinate updates will provide a better user experience.
184+
170185
## Passing state into functions
171186

172187
JavaScript is a _pass-by-value_ language — when you call a function, the arguments are the _values_ rather than the _variables_. In other words:

0 commit comments

Comments
 (0)