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

287
Visualizações
How to modify state in reducer.js to cause re-render of component?

There is a component down the line that uses hash but it's not re-rendering every time there is a change in the value of state.hash. How do I modify this code in reducer.js so that when state.hash is updated, the component that uses it is re-rendered from scratch?

import { createSlice } from "@reduxjs/toolkit";


const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    hash: null,
    d: null,
  },
  reducers: {
    update: (state, {payload}) => {
      const {hash, d} = payload
      state.hash = hash
      state.d = d
    }
  }
})

export const {update} = counterSlice.actions

export const {reducer: counterReducer} = counterSlice

Alternatively, how do I change this code that calls the reducer to achieve the same effect?

  store.dispatch(update({hash, d: d.toString()}))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the useEffect hook to listen for changes to a variable in state and re-render the component when it changes.

useEffect(() => {
  console.log(hash)
},[state.hash])

Reference

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