Component events

Components can also dispatch events. To do so, they must create an event dispatcher. Update Inner.svelte:

  1. <script>
  2. import { createEventDispatcher } from 'svelte';
  3. const dispatch = createEventDispatcher();
  4. function sayHello() {
  5. dispatch('message', {
  6. text: 'Hello!'
  7. });
  8. }
  9. </script>

createEventDispatcher must be called when the component is first instantiated — you can’t do it later inside e.g. a setTimeout callback. This links dispatch to the component instance.