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

211
Visualizações
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
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