简体   繁体   English

如何在 React Hooks 和 useRouteMatch 中创建将用户发送到 id 页面的链接

[英]how to create a link that send the user to the id page in React Hooks and useRouteMatch

I am intermediate with React Hooks and trying to understand how to use useRouteMatch or any good solution to achieve the case below.我是 React Hooks 的中间人,并试图了解如何使用useRouteMatch或任何好的解决方案来实现以下情况。

I have looked online and different examples but i cant seem to figure out based on a list of items, how to create a link that a user would click then get sent to the id page ex: book/1/ after clicking on one book on page books/我查看了在线和不同的示例,但我似乎无法根据项目列表弄清楚如何创建用户单击然后发送到 id 页面的链接, ex: book/1/单击一本书后books/

Data.js数据.js

export const books = [
    {
        id: 1,
        book_name: "A new year, a new era",
    },
    {
        id: 2,
        book_name: "You got to start somewhere",
    },
    {
        id: 3,
        book_name: "The tale of a gemini",
    },
]

App.js应用程序.js

import { books } from './components/Data';

function App() {

  const booksData = books;

  return (
    <div className="header">
      {booksData.map((data) => (
      <div className="book-id" key={data.id}>
        <div className="book-name"><b>{`${data.book_name}`}</b>
        </div><br/><br/>
      </div>))}
    </div>
  );
}

export default App;

If anybody could show me how to do it from the example above, i would be able to understand that clearly.如果有人可以从上面的示例中向我展示如何做到这一点,我将能够清楚地理解这一点。

Any help would be really appreciated.任何帮助将非常感激。

Thanks in advance.提前致谢。

Are you just looking for the Link component?您只是在寻找Link组件吗?

import { Link } from 'react-router-dom';

// later in your code...
<Link to="/books/1">Book 1</Link>

// or you can do it dynamically like so:
<Link to={`/books/${book.id}`}>Book {book.id}</Link>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM