Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda