Linking to routes using the <Link> component
When dealing with links that the user can click to visit a different page, it is best and easiest to use the Link component. This component will automatically create a simple link to a specific page for us.
Let’s get started using the Link component to provide a link to a single post:
- Create a new
src/components/post/PostListItem.jsxfile, in which we are going to define a simplified version of thePostcomponent, which will be shown in thePostListcomponent. Inside it, import theuseContextfunction, theThemeContextand theLinkcomponent fromreact-router:import { useContext } from 'react' import { ThemeContext } from '@/contexts/ThemeContext.js' import { Link } from 'react-router' - Define and export the
PostListItemcomponent, which accepts the postid,title, andauthoras props:export function PostListItem({ id, title, author }) {
...