在React中,当从列表中选择组件时,我该如何显示完整的项目信息?

huangapple go评论90阅读模式
英文:

In React, how can I show full item information when selecting a component from a list?

问题

我是新手编程,也是新手使用React和JavaScript!
我需要一点关于编码任务的帮助,我的主管给了我一个任务,要制作一个可以分类和扫描带有条形码的物品的应用程序。
所以我有一个物品列表,这些物品是在我的“Item”组件中创建的,它们在屏幕上的预览中包括注册日期、编号以及一些其他信息。

一旦我有了这个列表,我的目标是使任何一个物品都可以被点击,然后在新窗口中访问其完整信息。

我知道最接近我的目标的是一个模态窗口... 但这将牺牲屏幕上的宝贵空间,而我需要渲染大量信息。

这就是为什么我需要你的帮助!

这是我的代码,提前感谢你的帮助!

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'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, which are made in my "Item "component, with their date of registration, number id, and some other information 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 information.

The only thing I know which 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 &quot;./DummyItem.css&quot;
import DummyDate from &quot;./DummyDate&quot;

export default function DummyItem(props) {


  return (
    &lt;div className= &quot;dummy-item&quot;&gt;
      {/* &lt;DummyDate date={&quot;20/12/2339&quot;} /&gt; */}
      &lt;DummyDate /&gt;
      &lt;div className={&quot;dummy-item__description&quot;}&gt;
        &lt;h2&gt;{props.description}&lt;/h2&gt;
        &lt;p&gt;Annotations: {props.annotation}&lt;/p&gt;
        &lt;button type=&quot;button&quot; className=&quot;dummy-item__inventory&quot; onClick={() =&gt; alert(&#39;Show Details&#39;)}&gt;Inventory ID: {props.InventryID}&lt;/button&gt;

      &lt;/div&gt;

    &lt;/div&gt;
  )
}

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, which 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.

答案1

得分: 0

你可以使用 React Router 来创建新页面。使用 "useNavigate"。

英文:

You can do it with React Router if you want a new page. With "useNavigate"

答案2

得分: 0

为了实现在点击时在新页面中显示项目的完整详情的目标,您可以使用React Router来处理导航,并创建一个单独的组件用于项目详情页面。

以下是如何修改您的代码来实现这一目标的示例:

首先,确保您已经在项目中安装了React Router。您可以在项目目录中运行以下命令来安装它:

npm install react-router-dom

创建一个新的组件用于项目详情页面。让我们称之为ItemDetails.js。这个组件将接收项目详情作为props并显示它们:

import React from "react";

export default function ItemDetails(props) {
  return (
     <div>
       <h2>{props.description}</h2>
       <p>Annotations: {props.annotation}</p>
       {/* 显示其他项目详情 */}
    </div>
  );
 }

更新您的DummyItem组件,以使用React Router的Link组件代替按钮。用Link组件替换按钮元素,并将项目详情作为URL参数传递:

import React from "react";
import { Link } from "react-router-dom";

export default function DummyItem(props) {
  return (
    <div className="dummy-item">
      {/* 您的项目预览内容 */}
      <Link to={`/item/${props.itemId}`}>
        <div className="dummy-item__description">
          <h2>{props.description}</h2>
          <p>Annotations: {props.annotation}</p>
        </div>
      </Link>
    </div>
  );
}

更新您的App.js文件,使用React Router定义路由。添加一个新的路由,映射到ItemDetails组件:

import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Home from "./pages/Home";
import ItemDetails from "./pages/ItemDetails";

export default function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/item/:itemId" element={<ItemDetails />} />
      </Routes>
    </Router>
  );
}

在您的Home组件(或任何您渲染项目列表的地方),确保将必要的props传递给DummyItem组件,包括项目ID:

import React from "react";
import DummyItem from "../components/DummyItem";

export default function Home() {
  // 假设您有一个项目数组
  const items = [
    {
      itemId: 1,
      description: "项目 1",
      annotation: "注释 1",
      // 其他项目详情
    },
    // 其他项目
  ];

  return (
    <div>
      {items.map((item) => (
        <DummyItem
          key={item.itemId}
          itemId={item.itemId}
          description={item.description}
          annotation={item.annotation}
          // 将其他项目详情作为props传递
        />
      ))}
    </div>
  );
}

通过这些修改,当您点击列表中的项目时,React Router将导航到项目详情页面(/item/:itemId)并呈现ItemDetails组件,显示所选项目的完整详情。

英文:

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:

npm install react-router-dom

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:

import React from &quot;react&quot;;

export default function ItemDetails(props) {
  return (
     &lt;div&gt;
       &lt;h2&gt;{props.description}&lt;/h2&gt;
       &lt;p&gt;Annotations: {props.annotation}&lt;/p&gt;
       {/* Display other item details */}
    &lt;/div&gt;
  );
 }

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:

import React from &quot;react&quot;;
import { Link } from &quot;react-router-dom&quot;;

export default function DummyItem(props) {
  return (
    &lt;div className=&quot;dummy-item&quot;&gt;
      {/* Your item preview content */}
      &lt;Link to={`/item/${props.itemId}`}&gt;
        &lt;div className=&quot;dummy-item__description&quot;&gt;
          &lt;h2&gt;{props.description}&lt;/h2&gt;
          &lt;p&gt;Annotations: {props.annotation}&lt;/p&gt;
        &lt;/div&gt;
      &lt;/Link&gt;
    &lt;/div&gt;
  );
}

Update your App.js file to define the routes using React Router. Add a new route that maps to the ItemDetails component:

import React from &quot;react&quot;;
import { BrowserRouter as Router, Routes, Route } from &quot;react-router-dom&quot;;
import Home from &quot;./pages/Home&quot;;
import ItemDetails from &quot;./pages/ItemDetails&quot;;

export default function App() {
  return (
    &lt;Router&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} /&gt;
        &lt;Route path=&quot;/item/:itemId&quot; element={&lt;ItemDetails /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/Router&gt;
  );
}

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:

import React from &quot;react&quot;;
import DummyItem from &quot;../components/DummyItem&quot;;

export default function Home() {
  // Assuming you have an array of items
  const items = [
    {
      itemId: 1,
      description: &quot;Item 1&quot;,
      annotation: &quot;Annotation 1&quot;,
      // Other item details
    },
    // Other items
  ];

  return (
    &lt;div&gt;
      {items.map((item) =&gt; (
        &lt;DummyItem
          key={item.itemId}
          itemId={item.itemId}
          description={item.description}
          annotation={item.annotation}
          // Pass other item details as props
        /&gt;
      ))}
    &lt;/div&gt;
  );
}

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.

huangapple
  • 本文由 发表于 2023年5月29日 18:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定