Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
Hover on object entries triggers unnecessary reactivity in Svelte

I have an object that I loop over with an each block. When I hover, I temporarily change a property on the actual object entry and use it to assign a class. This works fine, but it also causes other reactivity to be triggered unnecessarily often.

<script>
    let things = [
        { id: 1, name: 'apple' },
        { id: 2, name: 'banana' },
        { id: 3, name: 'carrot' },
        { id: 4, name: 'doughnut' },
        { id: 5, name: 'egg' },
    ];
    
    function makePretty(somethings) {
        const prettyThings = somethings.map(thing => thing.name).join(' ');
        console.log(prettyThings);
        return prettyThings;
    }
    
    $: prettyThings = makePretty(things)
</script>

<ul>
{#each things as thing (thing.id)}
    <li 
            class:hover={thing.hover}
            on:mouseenter={() => (thing.hover = true)}
            on:mouseleave={() => (thing.hover = false)}
    >
        {thing.name}
    </li>
{/each}
</ul>

<style>
    .hover {
        background-color: cyan;
    }
</style>

REPL: https://svelte.dev/repl/c658cccd0bc2471a8f9c4758387340c5?version=3.48.0 n the console, you can see how often the reactivity on the prettyThings is triggered when you move over the list with your mouse.

I do realise that this is expected behaviour, as the 'things'-object is effectively changed on every mouseenter and mouseleave. And thus the prettyThings is called every time.

What would be an ideomatic way to apply a hover to a 'row' from an array of objects, without changing the object itself? In doing so, the object should still be live modifiable by other operations (e.g. add, delete, ...).

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

One way to do this, would be to split the rendering into a separate component.

<script>
    export let thing;
</script>

<li class:hover={thing.hover}
    on:mouseenter={() => (thing.hover = true)}
    on:mouseleave={() => (thing.hover = false)} >
    {thing.name}
</li>

<style>
    .hover {
        background-color: cyan;
    }
</style>
{#each things as thing (thing.id)}
    <ItemDisplay {thing} />
{/each}

REPL

Maybe there are better approaches, though.

about 4 years ago · Juan Pablo Isaza Relatório

0

EDIT: You could store the "Row Hovered" in a separate variable, then apply the style based on "HoveredRowId === thing.id"

<script>
    let things = [
        { id: 1, name: 'apple' },
        { id: 2, name: 'banana' },
        { id: 3, name: 'carrot' },
        { id: 4, name: 'doughnut' },
        { id: 5, name: 'egg' },
    ];
    
    let hoveredId = 0;
    
    function makePretty(somethings) {
        const prettyThings = somethings.map(thing => thing.name).join(' ');
        console.log(prettyThings);
        return prettyThings;
    }
    
    $: prettyThings = makePretty(things)
</script>

<ul>
{#each things as thing (thing.id)}
    <li 
            class:hover={thing.id === hoveredId}
            on:mouseenter={() => (hoveredId = thing.id)}
            on:mouseleave={() => (hoveredId = 0)}
    >
        {thing.name}
    </li>
{/each}
</ul>
<p>
    Hovered ID: {hoveredId}
</p>

<style>
    .hover {
        background-color: cyan;
    }
</style>

REPL: https://svelte.dev/repl/e157abc957874228983c9eafb2246aa3?version=3.48.0

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda