1

I'm trying to access an Object method outside the onMount, but I'm having an issue. I initialized the object inside the onMount, and I thought declaring the variable globally would solve the issue, but it didn't, so now I'm at a loss.

This is what I initially had. The code gave me an exception on slider.getInfo() saying that the object is possibly 'undefined.'

import { onMount } from "svelte";
import {tns} from "tiny-slider/src/tiny-slider.js";

let slider

onMount(()=>{
  slider = tns({
    container: ".my-slider",
    slideBy: 1,
    speed: 1000,
    nav: false,
    controls: false,
    autoplay: true,
    autoplayButtonOutput: false,
    responsive: {
      1024: {
        items: 4,
      }
    },
  })
})

let sliderInfo = slider.getInfo()

Next I tried putting it in a function and calling the function.

import { onMount } from "svelte";
import {tns} from "tiny-slider/src/tiny-slider.js";

let slider

onMount(()=>{
  slider = tns({
    container: ".my-slider",
    slideBy: 1,
    speed: 1000,
    nav: false,
    controls: false,
    autoplay: true,
    autoplayButtonOutput: false,
    responsive: {
      1024: {
        items: 4,
      }
    },
  })
})

function setInfo(){
  let sliderInfo = slider.getInfo()
  console.log(sliderInfo)
}
setInfo()

This time the code gave me the following error

Uncaught TypeError: Cannot read properties of undefined (reading 'getInfo')

Finally, just because I wanted to see if I could somehow get it to work, I created a button and assigned the function as a handler, and surprisingly this worked. Although I have no idea why this works compared to what I previously tried.

import { onMount } from "svelte";
import {tns} from "tiny-slider/src/tiny-slider.js";

let slider

onMount(()=>{
  slider = tns({
    container: ".my-slider",
    slideBy: 1,
    speed: 1000,
    nav: false,
    controls: false,
    autoplay: true,
    autoplayButtonOutput: false,
    responsive: {
      1024: {
        items: 4,
      }
    },
  })
})

function setInfo(){
  let sliderInfo = slider.getInfo()
  console.log(sliderInfo)
}

...

<button on:click={setInfo}>Click</button>

Unfortunately, the method that worked is not what I'm trying to do, but the first method instead. However I can't figure out why the first method doesn't work. Any help is appreciated.

1 Answer 1

3

onMount registers a callback, which is executed when the component mounted as DOM, that is, it will not execute immediately, so it will execute after the code you placed below it. (Whether it is wrapped in a function that is called immediately or not, does not matter.)

The click event on the other hand happens much later, after all the DOM already has to be there to interact with it, hence the callback within onMount has been called already and slider has been set.

How this should be dealt with depends on what you are trying to do. If you just need some code within the component to run, you can just add it to the onMount callback as well. If another component needs to do something, you can e.g. dispatch an event at the end of onMount.

Sign up to request clarification or add additional context in comments.

2 Comments

I just want to call the sliders getInfo() function outside of the onMount and set it to a variable (my first section of code). What would I do then? @H.B.
Why would you have to do that outside? Just declare the variable like slider and assign it in onMount. Also, setting a variable is not a goal, it's a means to a goal, it is not what matters here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.