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

209
Views
React with TS - useReducer with (immer) produce npm package, state does not update inside component

Here is the reducer -

const reducer = produce((draft: State = initialState, action: actions.CompanyTypes) => {

  if (action.type === 'SELECT_COUNTRY') {
    draft.countryName = action.payload.countryName;
  }

  if (action.type === 'CHANGE_SELECTION') {

    const countriesData = transformData(countries)

    draft.countriesData = countriesData;
  }
});

I'm using a packege called react-simple-map here is where I'm dispatching -

                <Geography
                  key={geo.rsmKey}
                  geography={geo}
                  onMouseEnter={() => {
                    const countryName = geo.properties.NAME;
                    dispatch({type: SELECT_COUNTRY, payload: { countryName }});
                  }}
                  onMouseLeave={() => {
                    dispatch({type: SELECT_COUNTRY, payload: {countryName: ""}});
                  }}
                  style={{
                    default: {
                      fill: countriesState!.countriesData[geo.properties.NAME]?.opacity ? "#34047d" : "#b9b9bd",
                      outline: "none",
                      opacity: Number(countriesState!.countriesData[geo.properties.NAME]?.opacity) + '%',
                    },
                    hover: {
                      fill: "#34047d",
                      outline: "none",
                    },
                  }}
                />

Here when I'm changing the state I don't get anything -

const MapView: React.FC<Props> = (props: React.PropsWithChildren<Props>) => {
  const [ countriesState, dispatch ] = useReducer(reducer, initialState);

  console.log(countriesState!.countryName);

  return (
    
  );
};

Why is that? Would love to here a solution.

about 4 years ago · Juan Pablo Isaza
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!