How I can reassign value inside of onMount() in sveltekit?

25 views Asked by At

This may be an already asked question but I cannot find it.

I want to reassign a variable using onMount. However, it still return undefined after I called the function.

Here's my code:

import { onMount } from 'svelte';

let selectedDate;

    function showEventIndicator(){
        const containerIndicator = dateContainer.querySelectorAll('.event-indicator');

        //map all elements in calendar 
        let datesFromCalendar = new Map();      
        
        // loop through elements
        containerIndicator.forEach((element:Element, index: number )=> {
         
         //get the data-value that has aria-selected attribute which its value is true
         if (element.parentNode instanceof Element && element.parentNode.getAttribute('aria-selected') == 'true'){
            selectedDate = element.parentNode.getAttribute('data-value'); //assign value to the global variable
            console.log(selectedDate)    
          }
        });
}

onMount(showEventIndicator)

console.log(selectedDate); // problem: value is still undefined :(

0

There are 0 answers