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:
parent
707455fa8b
commit
afd3f4e5a9
@ -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…
Reference in New Issue
Block a user