0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

[feat] Make setContext return the value that was passed in (#7432)

* return value from setContext

* update docs

* Add test

* eof new line

* pacify the linter

* const and tabs
This commit is contained in:
gtmnayan 2022-04-11 06:35:28 +05:45 committed by GitHub
parent 707455fa8b
commit afd3f4e5a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 2 deletions

View File

@ -148,7 +148,7 @@ setContext(key: any, context: any)
---
Associates an arbitrary `context` object with the current component and the specified `key`. The context is then available to children of the component (including slotted content) with `getContext`.
Associates an arbitrary `context` object with the current component and the specified `key` and returns that object. The context is then available to children of the component (including slotted content) with `getContext`.
Like lifecycle functions, this must be called during component initialisation.

View File

@ -46,8 +46,9 @@ export function createEventDispatcher<
};
}
export function setContext<T>(key, context: T) {
export function setContext<T>(key, context: T): T {
get_current_component().$$.context.set(key, context);
return context;
}
export function getContext<T>(key): T {

View File

@ -0,0 +1,5 @@
export default {
html: `
<div>true</div>
`
};

View File

@ -0,0 +1,7 @@
<script>
import { setContext } from 'svelte';
const a = {};
const b = setContext('foo', a);
</script>
<div>{a === b}</div>