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

149
Visualizações
async onClick with multiple setStates not working as expected

BACKGROUND

I have a object that I am referencing to toggle state changes in an onClick event.

const map = {
      active: {
        name: "sliceA",
        toggle: (map) => map.inactive
      },
      inactive: {
        name: "sliceB",
        toggle: (map) => map.active
      }
    }

useState of component either holds map.acive or map.inactive slice of object via a transition function.

const [slice, setSlice] = useState(map.active);

const transition = (getMethod) => {
    const getNewSlice = getMethod(slice);
    const result = getNewSlice(map);
    setSlice(result);
  };

This works as I expect until you attempt multiple state changes in a single onClick event.

const handleClick = async () => {
    try {
      transition(slice => slice.toggle);
      await stall();
      transition(slice => slice.toggle);
    } catch (err) {}
  };

ISSUE

See link - codesandbox1

If you click button X1 the 2nd transition() in handleClick() will not trigger state change.

If you click button X2 the 2nd transition() in handleClick() will trigger state change.

REQUEST

Why is this happening and is there a way to fix? I made another more manual way of setting state and it operates as expected - codesandbox2

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The reason is both transition calls in one handleClick call refer to the state of slice from the render call transition was defined in. In other words, setState doesn't update the slice the transtion has a closure over.

To make it always act on the current state, use a callback setter. currentSlice will be updated between render calls as well:

const transition = (getMethod) => {
  setSlice((currentSlice) => {
    const getNewSlice = getMethod(currentSlice);
    return getNewSlice(map);
  });
};

Sandbox link

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