Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda