Function called outside component initialization
Asked Answered
S

1

15

i wanna to recall the onMount in my Svelte app by clicking some button, but i got this error. Any idea would be amazing, thx :)

attemp

Superfine answered 16/7, 2021 at 5:23 Comment(0)
H
13

This does not work. Like the error says, you must call onMount during initialization of the component. If you need to invoke the function inside onMount multiple times afterwards, pull out the code into its own function.

<script>
  import { onMount } from 'svelte';

  function someFunction() {
    // do stuff
  }

  onMount(someFunction)
</script>

<button on:click={someFunction}>Click me</button>
Hiroshige answered 16/7, 2021 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.