Not sure about getContext. I vaguely recall looking into it and figuring out that getContext expects to be called as part of a render cycle of a component tree, which you can’t easily fake.
If you’re comfortable with relying on internal APIs like $on then one thing you could do is use the tutorial view for context (svelte.dev/tutorial/context-api). Choose JS output and explore the JS code that Svelte products for your component. Dig through to learn how setContext and getContext work. It might give you a solution. Failing that, use the Svelte source on GitHub. Let me know if you figure something out--I’d be interested to know.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I've tried it out, works like a charm!
One more question: is it possible to test a component with
getContext
without a wrapper component?Here is an example: github.com/blacksonic/todoapp-svel...
Glad to hear it worked! 🎉
Not sure about
getContext
. I vaguely recall looking into it and figuring out thatgetContext
expects to be called as part of a render cycle of a component tree, which you can’t easily fake.If you’re comfortable with relying on internal APIs like
$on
then one thing you could do is use the tutorial view for context (svelte.dev/tutorial/context-api). ChooseJS output
and explore the JS code that Svelte products for your component. Dig through to learn howsetContext
andgetContext
work. It might give you a solution. Failing that, use the Svelte source on GitHub. Let me know if you figure something out--I’d be interested to know.