Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!