flekschas / svelte-simple-modal

A simple, small, and content-agnostic modal for Svelte v3 and v4
https://svelte.dev/repl/b95ce66b0ef34064a34afc5c0249f313
MIT License
422 stars 30 forks source link

Uncaught (in promise) TypeError: Cannot destructure property 'open' of 'getContext(...)' as it is undefined. #111

Closed impossibletower closed 8 months ago

impossibletower commented 9 months ago

I am getting this error: Uncaught (in promise) TypeError: Cannot destructure property 'open' of 'getContext(...)' as it is undefined.

The code runs fine even though it's throwing that error. Cannot for the life of me figure out why it's throwing it.

I followed the instructions where it's inside a wrapper, and use import { getContext } from 'svelte'; const { open, close } = getContext('simple-modal');

KuyaKoya commented 8 months ago

I resolved mine by adding a generic type to getContext

import type { Modal } from 'svelte-simple-modal';

const { open, close } = getContext<Modal>('simple-modal');
flekschas commented 8 months ago

Yeah, getContext doesn't automatically know what the context type is so you have to specify it as @KuyaKoya showed.

You can also use the special Context type as it defines only the two exposed functions:

import type { Context } from 'svelte-simple-modal';

const { open, close } = getContext<Context>('simple-modal');