I've started working with Svelte and I stumbled upon some unexpected behavior. This is the snippet of my app source
let currentPage = 1; $: if (currentPage || $selectedOption) { fetchPage(currentPage, $selectedOption); } async function fetchPage(page: number, option: string) { //... } onMount(() => { fetchPage(currentPage, $selectedOption); });
When I load the page for the first time fetchPage()
function is called twice. However, if I'll make this change let currentPage = 0;
it is properly called only once. The problem is that I need to have currentPage
value initially set to 1.
I've tried also this change:
let currentChange = 0;// ... rest the sameonMount(() => { fetchPage(currentPage, $selectedOption); currentPage = 1; });
But this didn't help as well. Similarly any bool flags set in onMount()
and then checked in if statement didn't help.
Any suggestion what else can I do?