Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Svelte Actions Are Not Working With Non-Svelte JavaScript Libraries

I am having a list of blog cards which I have converted to a carousel using Splide. Whenever I change to another page, I want to remove the existing Splide object. I want to use Svelte actions to achieve this but am unable to do it.

<script>
  const splideAction = async (node, props)=>{
    
        const module = await import("@splidejs/splide");
        const Splide = module.default;
        const splide_css = await import("@splidejs/splide/dist/css/splide.min.css");
        const carousel = new Splide(".splide",{
        perPage:3,
        type:"loop",
        rewind: true,
        permove: 1,
        autoplay: true,
        breakpoints: {
            750: {
                perPage: 1
            },
            800:{
                perPage: 2
            }
        }
    }).mount();
        return {
            updated(){
                carousel.update();
                console.log("Updated")
            },
            destroy(){
                carousel.destroy();
                console.log("destroy")
            }
        }
    }
</script>


<div class="splide" use:splideAction>
                    <div class="splide__track" >
                        <div class="splide__list" >
                            {#each [...metadata.related_articles] as article (article.id)}
                            <div  class="splide__slide flex"><Card 
                            title = "{article.title}"
                            date = "{article.date}"
                            slug = "{article.slug}"
                            category= "{article.category}"
                            image = "{article.image}"/></div>
                                        
                        {/each} 
                        </div>
                    </div>
                        
                </div>

When I click the card to another blog posts, I generates a new Carousel and the Old one is still there, How do I remove it destroy

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A few things:

  1. I would import the Splide module synchronously in your component instead of a dynamic import, so it is ready as soon as the component is rendered.
  2. I'm not sure if asynchronously importing the CSS will work like that -- it may depend on your bundler setup. Either way, I would import it outside of the action, either by bundling it with the rest of your CSS or via CDN. In my example below, I load it from CDN using <svelte:head>.
  3. I don't see an "updated" function in Splide's API, so I'm not sure what carousel.update() would do. I don't think you need to return an "update" method from your action anyway, since your action isn't taking any parameters. The update method is meant to react to your action's parameters changing.
  4. You're incorrectly storing a reference to the carousel -- you're storing a reference to the return value of mount, not the constructor. Because of this, destroy is being called on the wrong variable. Try something like this:
const carousel = new Splide();
carousel.mount();

While your question is missing some context, the following component seems to work. I simplified the template since I don't have access to the article data. You can view it live in the Svelte REPL. I added a button to remove the component so you can see the carousel get destroyed.

<script>
    import Splide from '@splidejs/splide';
    
    function carousel(node) {
        const splide = new Splide(node, {
                perPage: 3,
                type: "loop",
                rewind: true,
                permove: 1,
                autoplay: true,
                breakpoints: {
                    750: {
                        perPage: 1
                    },
                    800: {
                        perPage: 2
                    }
                }
        });
        splide.mount();
        
        return {
            destroy: () => {
                splide.destroy();
                console.log('destroyed');
            }
        }
    }
</script>

<svelte:head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@splidejs/splide@3.0.9/dist/css/splide.min.css">
</svelte:head>

<div class="splide" use:carousel>
  <div class="splide__track">
        <ul class="splide__list">
            <li class="splide__slide">Slide 01</li>
            <li class="splide__slide">Slide 02</li>
            <li class="splide__slide">Slide 03</li>
        </ul>
  </div>
</div>

<style>
    li {
        height: 300px;
        background: lightblue;
    }
</style>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!