[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
pull/6583/head
gtmnayan 2 years ago committed by GitHub
parent 707455fa8b
commit afd3f4e5a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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.

@ -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 {

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

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