support passing context into ssr component api

pull/6032/head
Tan Li Hau 6 years ago committed by tanhauhau
parent 36cd61f0c6
commit adf6085d9c

@ -1082,3 +1082,22 @@ const { head, html, css } = App.render({
answer: 42 answer: 42
}); });
``` ```
You can call the `.render()` with the following parameters:
| option | default | description |
| --- | --- | --- |
| `props` | `{}` | An object of properties to supply to the component
| `slots` | `{}` | A object of slots to supply to the component
| `context` | `new Map()` | A `Map` of context key-value pairs to supply to the component
```js
const { head, html, css } = App.render(
// props
{ answer: 42 },
// slots
{ 'slot-a': (lets) => 'hello world' },
// contexts
new Map([['context-key', 'context-value']]),
);
```

@ -74,12 +74,12 @@ export function debug(file, line, column, values) {
let on_destroy; let on_destroy;
export function create_ssr_component(fn) { export function create_ssr_component(fn) {
function $$render(result, props, bindings, slots) { function $$render(result, props, bindings, slots, context) {
const parent_component = current_component; const parent_component = current_component;
const $$ = { const $$ = {
on_destroy, on_destroy,
context: new Map(parent_component ? parent_component.$$.context : []), context: new Map(parent_component ? parent_component.$$.context : context || []),
// these will be immediately discarded // these will be immediately discarded
on_mount: [], on_mount: [],
@ -97,7 +97,7 @@ export function create_ssr_component(fn) {
} }
return { return {
render: (props = {}, options = {}) => { render: (props = {}, slots = {}, context = new Map()) => {
on_destroy = []; on_destroy = [];
const result: { const result: {
@ -109,7 +109,7 @@ export function create_ssr_component(fn) {
}>; }>;
} = { title: '', head: '', css: new Set() }; } = { title: '', head: '', css: new Set() };
const html = $$render(result, props, {}, options); const html = $$render(result, props, {}, slots, context);
run_all(on_destroy); run_all(on_destroy);

@ -18,5 +18,15 @@ export default {
assert.deepEqual(called, ['hello world']); assert.deepEqual(called, ['hello world']);
component.$destroy(); component.$destroy();
},
test_ssr({ assert }) {
const Component = require('./Component.svelte').default;
const called = [];
const { html } = Component.render(undefined, undefined, new Map([
['key', 'svelte'],
['fn', (value) => called.push(value)]
]));
assert.htmlEqual(html, '<div>svelte</div><button></button>');
} }
}; };

Loading…
Cancel
Save