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

305
Vistas
Update values used in array based on update in store (svelte)?

In a nutshell, I am building a web app like mycolor.space, where you type in the hex value of a color and the application spits out a bunch of color palettes using a library called tinycolor, which does some simple lightening/darkening/rotation of hues.

So I'm able to do handle all the binding and subscriptions to the store value that we'll call primaryColor. primaryColor successfully updates when the user types in a hex code into an input field. I have a svelte component called Palettes.svelte which looks something like this:

<script>
import tiny color from 'tinycolor2'

export let color

const palettes = [
  {
    name: 'Simple Gradient',
    colors: [
      tinycolor(color).colorMutationFunction1(),
      tinycolor(color).colorMutationFunction2(),
      .
      .
      .
      ]
  },
  .
  .
  .

]
</script>
<main>
  <h1>{color}</h1>
  {#each palettes as palette}
    {#each palette.colors as color}
      <div>{color}</div>
    {/each}
  {/each}
</main>

So the core functionality is there. It produces color palettes based on the initial store value, which lives inside the array from the getgo. The problem is that the value color inside of the palettes array does not update reactively when the user changes the hex color value in the input. The h1 tag in the main body of this component predictably does update alongside the store. I have read here and there that in order to iteratively change array values based on a change in the state of a store variable in svelte that you have to do some weird syntax that sets the array to itself, like palettes = palettes. However, I have only ever seen this in the context of a function.

Please let me know if any more information would be of any insight. The rest of my app is updating and passing the store variable just fine, so I know I'm just missing something fundamental about how to handle this.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In order to do this you have to explicitly mark palettes as reactive, this means that it will be recalculated if the values it is based on change.

The general syntax for this is $: a = b where a will always update whenever b (or anything else on the righthand side of the equation) changes.

In your case the syntax would become:

$: palettes = [
  {
    name: 'Simple Gradient',
    colors: [
      tinycolor(color).colorMutationFunction1(),
      tinycolor(color).colorMutationFunction2(),
  }
]

Since color appears on the right hand, palettes will be recalculated when the color changes.

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