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

300
Visualizações
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 Respostas
Responde à pergunta

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 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