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

169
Visualizações
Adding loading state to map function causes items to display as loading

I have a map function displaying items:

 {data.map((item, index) => ())}

Within this function I would like to have an onClick that conditionally displays a loading state when loading is true.

const [loading, setLoading] = useState(false)

{data.map((item, index) => (
   <span
     onClick={() =>
     handleDelete()}>
  {loading ? (
    <LoadingSpinner />
       ) : (
    <TrashIcon />)} 
</span>
))}

When I do this it causes all items in the list to display the loading state, not just the item clicked on.

Is there a way I can achieve this using the index?

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

0

All your map items are listening to a single boolean state value, instead, you can keep track of the index of the clicked item, or if you want to be able to click and show the loading state for multiple items you can use an array or Set.

Below is an approach using Set

const [loadingIndices, setLoadingIndices] = useState(new Set());
{data.map((item, index) => (
   <span
     onClick={() =>
     handleDelete(index)}>
  {loadingIndices.has(index) ? (
    <LoadingSpinner />
       ) : (
    <TrashIcon />)} 
</span>
))}

Now in your handleDelete function, you can add index of the clicked element to the set in state.

handleDelete = (selectedIndex) => {
  setLoadingIndices((prev) => new Set([...prev, selectedIndex]));
  // ...
  // ...
  // And to remove the element from loading state
  setLoadingIndices((prev) => {
    const updated = new Set(prev);
    updated.delete(selectedIndex);
    return updated;
  });
};
about 4 years ago · Juan Pablo Isaza Relatório

0

You should move your state in to the items instead of the parent. Updating state inside the parent component will cause all children to re-render.

const childComponent = (props) => { // you can pass the main loading state as a starting point
const [loading,setLoading] = useState(props.loading) // initial loader state
if(loading) return <LoadingSpinner />
return (
   <button onClick={()=> setLoading(prev => !prev)} /> 
)}

you can then mutate this to display different elements using if statements like above.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you don't mind the re-render of children, You can use this index logic.

const [loading, setLoading] = useState(null);

const handleDelete = (index) => {
    setLoading(index);
}

{data.map((item, index) => (
   <span
     onClick={() =>
      handleDelete(index)}>
  {loading === index ? (
    <LoadingSpinner />
       ) : (
    <TrashIcon />)} 
</span>
))}
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