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

185
Visualizações
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 Respostas
Responde à pergunta

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 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