Skip to content
Advertisement

Svelte: Get User Input Callback via Store Function

What Iโ€™m trying to do here may not be possible, but as a newcomer to Svelte, I hope it is. ๐Ÿ˜„

I have a delete button in a component that opens a globally available modal that serves as a confirmation dialog. The modal component is in my __layout.svelte so I can invoke it from anywhere in my app.

JavaScript

Here is my modal store:

JavaScript

Last of all, here is my component in my app where I actually initiate the delete process by clicking a link that shows the delete confirmation modal:

JavaScript

Iโ€™m unclear on how to connect a callback function through my confirmTrash function to pass the userโ€™s response in the modal back to where the modal was invoked. Is this possible?

Advertisement

Answer

For this to work, you just have to pass in function and call it accordingly.

JavaScript

Then call it in the component:

JavaScript

REPL example

I would not use singleton component like this but create new instances using the client-side component API. It is less redundant, leads to a cleaner life cycle and less unnecessary global state.

Example of that:

JavaScript
JavaScript

Usage:

JavaScript

REPL example

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement