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

116
Vistas
target specific HTML element from an object that is mapped

So I have replicated my issue on a smaller level so its easier to work with. The goal is to have an onclick function that shows the content inside the column which is hidden as default, this will ideally be handled through setting display: none and changing to display: flex. I have to use an object hence lodash. I cant use document.getElement because all mapped components share a common classname. The data comes from a database so the amount of columns is unknown and always changing per user requirements.

I guess im looking for a method to target to the classname of the div/tag of the iteration of the map where the onclick for that container is. Any help is appreciated!

import _ from "lodash"
import React from 'react'

const dummyData = {
    columnOne: "item one",
    columnTwo: "item two",
    columnThree: "item three"
}

function TestPage() {
    return (
        _.map(dummyData, (data, key) => {
            return (
                <>
                    <div className="column-container">
                        <p className="heading">{key}</p>
                        <div className="show-content-btn">V</div> //button to toggle content visibility
                    </div>
                    <p className="content">{data}</p> //content to be hidden/shown
                </>
            )
        }

        )
    )
}


export default TestPage
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use useState to store classnames and then toggle them with onClick function by changing state with setState.

See this answer. I think it contains what you want.

about 4 years ago · Juan Pablo Isaza Denunciar

0

One way of doing this is by making a separate component for HTML which return from map function and handle state in it. Like:

import _ from "lodash";
import React from "react";

const dummyData = {
  columnOne: "item one",
  columnTwo: "item two",
  columnThree: "item three"
};

function Data({ id, data }) {
  const [show, setShow] = React.useState(false);
  return (
    <>
      <div className="column-container">
        <p className="heading">{id}</p>
        <div className="show-content-btn">
          <button onClick={() => setShow(!show)}>V</button>
        </div>
      </div>
      {show && <p className="content">{data}</p>}
    </>
  );
}
function TestPage() {
  return _.map(dummyData, (data, key) => {
    return <Data key={key} id={key} data={data} />;
  });
}

export default TestPage;
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