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

202
Visualizações
React Pass the ID of clicked Element to another Component

My App.js have this structure.

 return (
<Container fluid="true" className="h-100">
  <Header />
  <Row className="contentRow">
    <CustomerList />
    <DetailPage />
  </Row>
</Container>
);

There are many elements in CustomerList. With a click I want to send the ID of the element to DetailPage and display the details of the associated element. But I am still quite new in react and don't really know how to pass the data. Or if I even need to change something in the structure of the components.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to define a new state variable in your component.

Then please pass it with the setter function into CustomerList.

Define state variable.

const [id, setId] = useState(null);

Then pass setter function into <CustomerList />

<CustomerList setId={setId} />

// on CustomerList click event

const onClick = (event) => {
   // your logic and use setId from props.
   // This is just an example.
   props.setId(event.target.value);
}

Finally, pass id state variable into <DetailPage /> so that your DetailPage component uses in its props.

<DetailPage id={id} />

Usage in Detailpage:

const DetailPage = (props) => {
   const id = props.id;
   // use id for your purpose.
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the event.target.id property. add an onclick function:

onClick={(e) => handleClick(e)}

handleClick = (e) => {
   //access e.target.id here
}
about 4 years ago · Juan Pablo Isaza Relatório

0

See if this help you:

import React, { useState } from "react";

const Header = () => <div />;

const CustomerList = ({ onChange }) => (
  <ul>
    {["item1", "item2", "item3", "item4"].map((item) => (
      <li onClick={() => onChange(item)} key={item}>
        {item}
      </li>
    ))}
  </ul>
);

const DetailPage = ({ selectedItem }) => <div>{selectedItem}</div>;

const Component = () => {
  const [selectedItem, setSelectedItem] = useState(null);

  const handleChange = (item) => {
    setSelectedItem(item);
  };

  return (
    <div> {/* Container */}
      <Header />
      <div className="contentRow"> {/* Row */}
        <CustomerList onChange={handleChange} />
        <DetailPage selectedItem={selectedItem} />
      </div>
    </div>
  );
};

When you click some item, we set the state in parent component, and then send to DetailPage, in DetailPage, you can use this selectedItem to show the info.You also can replace ["item1", "item2", "item3", "item4"] with an array of objects.

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