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

153
Visualizações
React one item's loading shows in every item in loop

I have a react page where I show all FAQs items in a list within a javascript map loop. There are two buttons yes or no. Users can vote an answer yes or no. So whenever the user clicks the buttons a loading indicator will show.

Faqs Page:

<>
{
faqs.map((faq) => (
 <Faq key={faq.id} faq={faq} />
))
}
</>

Faq component:

interface IProps {
  faq: IFaq;
}

const FaqVoteSection: React.FC<IProps> = (props) => {
  const { faq } = props;
  const dispatch = useDispatch();
  const { faqVoteLoading } = useSelector((state: AppState) => state.user);

  const [loadingState, setLoadingState] = useState<
    'yesvote-loading' | 'novote-loading' | 'not-loading'
  >('not-loading');

  const onVoteSubmit = async (vote: boolean) => {
    if (faq) {
      dispatch(
        faqVoteApiAction({
          faq_id: faq.id,
          vote,
        })
      );
      setLoadingState('not-loading');
    }
  };

  const onYesVotePress = () => {
    setLoadingState('yesvote-loading');
    onVoteSubmit(true);
  };

  const onNoVotePress = async () => {
    setLoadingState('novote-loading');
    await onVoteSubmit(false);
  };

  return (
    <FlexContainer>
      <SolidButton
        type={faq.vote === 'yes' ? 'primary' : 'default'}
        size="small"
        onClick={onYesVotePress}
        loading={loadingState === 'yesvote-loading' && faqVoteLoading}
      >
        Yes
      </SolidButton>
      <Gap />
      <SolidButton
        size="small"
        type={faq.vote === 'no' ? 'primary' : 'default'}
        onClick={onNoVotePress}
        loading={loadingState === 'novote-loading' && faqVoteLoading}
      >
        No
      </SolidButton>
    </FlexContainer>
  );
};

Now when I click a vote button I want to show the loading only for that item. But the loading shows every item in the list. I think it's because of the map loop.

enter image description here

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