Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function called outside component initialization

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

like image 730
Axtlan Avatar asked Jan 29 '26 04:01

Axtlan


1 Answers

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>
like image 115
dummdidumm Avatar answered Jan 31 '26 02:01

dummdidumm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!