Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!