woutdp / live_svelte

Svelte inside Phoenix LiveView with seamless end-to-end reactivity
https://hexdocs.pm/live_svelte
MIT License
1.01k stars 38 forks source link

Using live.handleEvent() #80

Closed absowoot closed 9 months ago

absowoot commented 9 months ago

When calling live.handleEvent(), I was receiving the following error: [Cannot read properties of undefined (reading 'handleEvent')]

After some trial and error, I found that I needed to import and wrap the function with onMount

<script>
    import { onMount } from "svelte";
    onMount(() => {
        live.handleEvent("my-event", (reply) => {});
    });
</script>

Is this intended or a bug? If intended, could you make a note in the readme?

woutdp commented 9 months ago

It's intended as it needs to be run on the client. If you're rendering SSR it will throw the error.

I'll add a note in the readme, thanks for reporting

woutdp commented 9 months ago

https://github.com/woutdp/live_svelte/commit/baf97ee09afa519902bb4d860376f94f0bef4772