Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: minor addition to onMount usage #445

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions content/tutorial/01-svelte/07-lifecycle/01-onmount/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: onMount

Every component has a _lifecycle_ that starts when it is created, and ends when it is destroyed. There are a handful of functions that allow you to run code at key moments during that lifecycle. The one you'll use most frequently is `onMount`, which runs after the component is first rendered to the DOM.

In this exercise, we have a `<canvas>` that we'd like to animate, using the `paint` function in `gradient.js`. Begin by importing the function from `svelte`:
In this exercise, we have a `<canvas>` that we'd like to animate, using the `paint` function in `gradient.js`. Begin by importing the `onMount` function from `svelte`:

```svelte
/// file: App.svelte
Expand All @@ -14,7 +14,7 @@ In this exercise, we have a `<canvas>` that we'd like to animate, using the `pai
</script>
```

Then, add a function that runs when the component mounts:
Then, pass a function to the `onMount` call that runs when the component mounts:
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved

```svelte
/// file: App.svelte
Expand All @@ -36,7 +36,7 @@ Then, add a function that runs when the component mounts:

> In a [later exercise](bind-this), we'll learn how to get an element reference without using `document.querySelector`.

So far so good — you should see gently undulating colours in the shape of the Svelte logo. But there's one problem — the loop will continue even after the component has been destroyed. To fix that, we need to return a cleanup function from `onMount`:
So far so good — you should see gently undulating colors in the shape of the Svelte logo. But there's one problem — the loop will continue even after the component has been destroyed. To fix that, we need to return a cleanup function from `onMount`:
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved

```js
/// file: App.svelte
Expand Down