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

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

Merged
merged 6 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion site/content/docs/03-run-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/internal/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions test/runtime/samples/context-setcontext-return/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
html: `
<div>true</div>
`
};
7 changes: 7 additions & 0 deletions test/runtime/samples/context-setcontext-return/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { setContext } from 'svelte';
const a = {};
const b = setContext('foo', a);
</script>

<div>{a === b}</div>