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

138
Visualizações
How to toggle item by id or index ? React.js

I need to opet child component by clicked item. FIrst check code:

    <div className="d-flex">
      {boardList.map((list) => (
        <div className="card m-3 p-3" key={list.id}>
          <div className="d-flex flex-column">
            <h6> {list.name} </h6>

            <ul className="list-group">
              {list.cards.map((card) => (
                <li className="list-group-item" key={card.id}>
                  {card.name}
                </li>
              ))}
            </ul> 

            {isVisible ? (
              <TodoForm onCloseForm={onCloseForm} />
            ) : (
              <small 
                className="mt-2"
                onClick={showInput}
              > 
                Add new task + 
              </small>
            )}
          </div>
        </div>
      ))}
    </div>

This is work but when I click on 'Add new task +' a child component opens up to me everywhere. i want only the component with the selected id or index to open.

also component for this :

  const [isVisible, setIsVisible] = useState(false);
  const [boardList, setBoardList] = useState([]);

  useEffect(() => {
    axiosInstance
      .get("")
      .then((res) => {
        setBoardList(res.data);
        console.log("resp", boardList);
      })
      .catch((err) => {
        console.log(err);
      });
  }, []);

  const showInput = () => {
    setIsVisible(true);
  };

  const onCloseForm = () => {
    setIsVisible(false);
  };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

All the items of the resultant array from boardList.map are depending on the same state isVisible, that's why when you click on one of them all the items mimic the same behaviour.

What you need is to create a component with its own state to encapsulate this part of your code

{isVisible ? (
  <TodoForm onCloseForm={onCloseForm} />
  ) : (
  <small 
    className="mt-2"
    onClick={showInput}
  > 
  Add new task + 
</small>
)}

This way every instance of this new component would have its own isVisible so they no longer would affect their siblings state.

The component would look like this.

const NewComponent = () => {
  const [isVisible, setIsVisible] = useState(false);

  return <>
    {isVisible ? (
        <TodoForm onCloseForm={onCloseForm} />
    ) : (
      <small className="mt-2" onClick={() => setIsVisible(true)}>
        Add new task +
      </small>
    )}
  </>
};
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