Skip to content

Commit 67b53bf

Browse files
authored
[fix] prefer context from constructor options (#6759)
1 parent cfc880b commit 67b53bf

File tree

8 files changed

+46
-2
lines changed

8 files changed

+46
-2
lines changed

src/runtime/internal/Component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function init(component, options, instance, create_fragment, not_equal, p
124124
on_disconnect: [],
125125
before_update: [],
126126
after_update: [],
127-
context: new Map(parent_component ? parent_component.$$.context : options.context || []),
127+
context: new Map(options.context || (parent_component ? parent_component.$$.context : [])),
128128

129129
// everything else
130130
callbacks: blank_object(),

src/runtime/internal/ssr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function create_ssr_component(fn) {
9191

9292
const $$ = {
9393
on_destroy,
94-
context: new Map(parent_component ? parent_component.$$.context : context || []),
94+
context: new Map(context || (parent_component ? parent_component.$$.context : [])),
9595

9696
// these will be immediately discarded
9797
on_mount: [],
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { getContext } from 'svelte';
3+
const value = getContext('foo');
4+
</script>
5+
6+
<div>Value in child component: {value}</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
skip_if_ssr: true,
3+
html: `
4+
<div><div>Value in child component: undefined</div></div>
5+
`
6+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import { setContext } from 'svelte';
3+
import ChildComponent from './ChildComponent.svelte';
4+
5+
setContext('foo', true);
6+
7+
function render(node) {
8+
new ChildComponent({
9+
target: node,
10+
context: new Map()
11+
});
12+
}
13+
</script>
14+
15+
<div use:render />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { getContext } from 'svelte';
3+
const value = getContext('foo');
4+
</script>
5+
6+
<div>Value in child component: {value}</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div><div>Value in child component: undefined</div></div>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import { setContext } from 'svelte';
3+
import ChildComponent from './ChildComponent.svelte';
4+
5+
setContext('foo', true);
6+
7+
const content = ChildComponent.render({}, { context: new Map() }).html;
8+
</script>
9+
10+
<div>{@html content}</div>

0 commit comments

Comments
 (0)