I’m new to coding, new to react and js!
I need a little help with a coding task my supervisor gave me in making an application to classify and scan items with a barcode.
So I got a list of items, wich are made in my "Item "component, with their date of registration, number id, and some other infos in their preview on screen.
Once I have this list, my objective is to make possible to click on any of them, and get access in a new window, to its full informations.
The only thing I know wich is closest to my target was a modal window… But this would sacrifice precious space on the screen, and I need to render a lot of information.
That’s why I need your help!
Here is my code, thanks for your help in advance!
import "./DummyItem.css"
import DummyDate from "./DummyDate"
export default function DummyItem(props) {
return (
<div className= "dummy-item">
{/* <DummyDate date={"20/12/2339"} /> */}
<DummyDate />
<div className={"dummy-item__description"}>
<h2>{props.description}</h2>
<p>Annotations: {props.annotation}</p>
<button type="button" className="dummy-item__inventory" onClick={() => alert('Show Details')}>Inventory ID: {props.InventryID}</button>
</div>
</div>
)
}
I’ve tried to make a modal window, but it has no sense.
And now my link to item details it’s located in a button, wich once clicked log on console.
I need to click in any point of my item preview to get access in a new "page" containing its
full details.
2
Answers
You can do it with React Router if you want a new page. With "useNavigate"
To achieve your objective of displaying the full details of an item in a new page when clicked, you can use React Router to handle the navigation and create a separate component for the item details page.
Here’s an example of how you can modify your code to implement this:
First, make sure you have React Router installed in your project. You can install it by running the following command in your project directory:
Create a new component for the item details page. Let’s call it ItemDetails.js. This component will receive the item details as props and display them:
Update your DummyItem component to use React Router’s Link component instead of a button. Replace the button element with the Link component and pass the item details as URL parameters:
Update your App.js file to define the routes using React Router. Add a new route that maps to the ItemDetails component:
In your Home component (or wherever you’re rendering the list of items), make sure you pass the necessary props to the DummyItem component, including the item ID:
With these modifications, when you click on an item in the list, React Router will navigate to the item details page (/item/:itemId) and render the ItemDetails component, displaying the full details of the selected item.