Svelte is a JavaScript framework used to develop web applications. It is unique because it runs the application on compile, and compiles the code in Vanilla JavaScript.
OnMount
is a function that is called when a component is initialized. In other words, we can say that when a component renders, the first function it calls is onMount
. This is similar to the concept of calling useEffect in React with an empty array as a parameter.
The following piece of code explains the syntax of onMount
function.
<script>import { onMount } from 'svelte';let array = [];onMount(async () => {const res = await fetch(`https://jsonplaceholder.typicode.com/photos?_limit=20`);array = await res.json();});</script>