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

183
Vistas
How to render components from an array on screen only when the corresponding button is clicked?

I need to show the objects in the array only when its button is clicked. So, I'm adding items to a list when each button is clicked. So I guess I need to add some kind of conditional logic?

const Calc = () => {
  const jobs = [
    { title: "Patio", body: <Patio /> },
    { title: "Fence", body: <Fence /> },
    { title: "Garden", body: <Garden /> },
    { title: "Footing", body: <Foot /> },
    { title: "Deck", body: <Deck /> },
  ];


  const [showOption, setShowOption] = useState(false);
  const optionClickHandler = () => {
    setShowOption(true);
  };

 

  return (
    <div className="Calc">
      <div>
        <BasicMenu />
      </div>

      <button onClick={optionClickHandler}>Footing</button>
      <button>Garden</button>
      <button>Fence</button>
      <button>Patio</button>
      <button>Deck</button>

      <div>
        <div>
          {jobs.map(({ title, body }) => {
            return (
              <div>
                <div>{title}</div>
                <div>{body}</div>
              </div>
            );
          })}
        </div>
      </div>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use a Set to keep track of the jobs that need to be shown and render the filtered jobs.

const jobs = [
  { title: "Patio", body: <Patio /> },
  { title: "Fence", body: <Fence /> },
  { title: "Garden", body: <Garden /> },
  { title: "Footing", body: <Foot /> },
  { title: "Deck", body: <Deck /> }
];

const Calc = () => {
  const [jobsToShow, setJobsToShow] = useState(new Set());

  const showJob = (jobTitle) => () => {
    setJobsToShow((prev) => new Set(prev).add(jobTitle));
  };

  const filteredJobs = useMemo(() => {
    return jobs.filter((job) => jobsToShow.has(job.title));
  }, [jobsToShow]);

  return (
    <div className="Calc">
      {jobs.map((job) => (
        <button key={job.title} onClick={showJob(job.title)}>
          {job.title}
        </button>
      ))}

      <div>
        <div>
          {filteredJobs.map(({ title, body }) => {
            return (
              <div key={title}>
                <div>title: {title}</div>
                <div>body: {body}</div>
                <hr />
              </div>
            );
          })}
        </div>
      </div>
    </div>
  );
};

Edit prod-pine-0kehji

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