Add notes to stress call-during-initialisation

pull/5631/head
Outvi V 5 years ago committed by GitHub
parent 33d1fc741a
commit 30ee548c01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -54,3 +54,25 @@ In fact, you might use the two together. Since context is not reactive, values t
```js ```js
const { these, are, stores } = getContext(...); const { these, are, stores } = getContext(...);
``` ```
## `getContext()` must be called during component initialisation
You may find this feature useful. However, remember to call `getContext()` during component initialisation! This means you can only use it directly in the `<script>` tag (for a `.svelte` file), not in any function. For example, this will work:
```js
import { getContext } from 'svelte';
const map = getContext('map');
```
While this will not:
```js
import { getContext } from 'svelte';
onMount(() => {
const map = getContext('map'); // will trigger an error!
});
```
You may find it work in some old Svelte versions, but it won't work for current versions of Svelte.

Loading…
Cancel
Save