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

664
Visualizações
(Why) does .forEach() mutate vuex state?

I grab two sets of balances from my vuex state and merge them into a new array of entries. why is an error thrown when i iterate over my copies of the vuex state? am i missing something?

.filter() returns a new array, so coins is not connected to the $state any more.

I'm not mutating the vuex state, but my copies of it. still i'm warned to not mutate vuex store outside of mutations.

this would make sense, if those assignments were actual references to the vuex store, but they're not (or are they??). I've tried to understand what's wrong here, but i can't understand what's wrong here. i hope you guys can help. thanks!

filteredItems() {
      let coins = this.$store.state.wallet.balances.filter(( item ) => this.getCoinPropertiesByTicker(item.ticker).staking_available);
      const stakingCoins = this.$store.state.wallet.stakingBalances;
->    coins.forEach((coin) => {     // error happening here
        const stakingCoin = stakingCoins.user.stats.find((name) => name.ticker === coin.ticker)
        if (stakingCoin) {
          coin.totalAmount = {
            amt: new BigNumber(stakingCoin.balance).decimalPlaces(8).toString(),
            usd: new BigNumber(stakingCoin.balance_usd).decimalPlaces(2).toString()
          }
        }
      return coins
      }



[Vue warn]: Error in callback for watcher "function () { return this._data.$$state }": "Error: [vuex] do not mutate vuex store state outside mutation handlers."

(found in <Root>)


vue.esm.js?a026:1897 Error: [vuex] do not mutate vuex store state outside mutation handlers.
    at assert (vuex.esm.js?2f62:90)
    at Vue.store._vm.$watch.deep (vuex.esm.js?2f62:793)
    at Watcher.run (vue.esm.js?a026:4577)
    at Watcher.update (vue.esm.js?a026:4551)
    at Dep.notify (vue.esm.js?a026:739)
    at Object.reactiveSetter [as amountStaked30] (vue.esm.js?a026:1064)
    at eval (Staking.vue?3a4a:134)
    at Array.forEach (<anonymous>)
    at VueComponent.filteredItems (Staking.vue?3a4a:115)
    at Watcher.get (vue.esm.js?a026:4488)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As others have posted (and i didn't know), the actual items inside the array are references to the store, even after assigning it to a variable.

in my case, i did JSON.parse(JSON.stringify(this.$store.state.wallet.balances)).filter(( item ) => this.getCoinPropertiesByTicker(item.ticker).staking_available), which creates a deep copy, and now the error is gone.

Thanks!

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