【发布时间】:2022-01-13 14:07:25
【问题描述】:
我有一个运行博客的 React 网站。
博客有 4 个不同的类别,按不同的页面排序。所以 Category1 有自己的页面,category2 有自己的页面,依此类推。
每篇博文都有一个“返回”按钮。现在,后退按钮只是将用户超链接回显示所有类别的索引页面。我尝试使用 React Router 将用户使用的上一页用作后退按钮的超链接,这样它就不会总是将他们从他们正在查看的特定类别中删除。
我似乎什么也做不了。有人有什么想法吗?
下面是组件的 JS 文件:
import React, { Fragment } from 'react'
import _get from 'lodash/get'
import { Link, graphql } from 'gatsby'
import { ChevronLeft, ChevronRight } from 'react-feather'
import Content from '../components/Content'
import Layout from '../components/Layout'
import './SinglePost.css'
export const SinglePostTemplate = ({
title,
date,
body,
nextPostURL,
prevPostURL,
categories = []
}) => (
<main>
<article
className="SinglePost section light"
itemScope
itemType="http://schema.org/BlogPosting"
>
<div className="container skinny">
<Link className="SinglePost--BackButton" to="/">
<ChevronLeft /> BACK
</Link>
<div className="SinglePost--Content relative">
<div className="SinglePost--Meta">
{date && (
<time
className="SinglePost--Meta--Date"
itemProp="dateCreated pubdate datePublished"
date={date}
>
{date}
</time>
)}
{categories && (
<Fragment>
<span>|</span>
{categories.map((cat, index) => (
<span
key={cat.category}
className="SinglePost--Meta--Category"
>
{cat.category}
{/* Add a comma on all but last category */}
{index !== categories.length - 1 ? ',' : ''}
</span>
))}
</Fragment>
)}
</div>
{title && (
<h1 className="SinglePost--Title" itemProp="title">
{title}
</h1>
)}
<div className="SinglePost--InnerContent">
<Content source={body} />
</div>
<div className="SinglePost--Pagination">
{prevPostURL && (
<Link
className="SinglePost--Pagination--Link prev"
to={prevPostURL}
>
<ChevronLeft /> Previous Post
</Link>
)}
{nextPostURL && (
<Link
className="SinglePost--Pagination--Link next"
to={nextPostURL}
>
Next Post <ChevronRight />
</Link>
)}
</div>
</div>
</div>
</article>
</main>
)
// Export Default SinglePost for front-end
const SinglePost = ({ data: { post, allPosts } }) => {
const thisEdge = allPosts.edges.find(edge => edge.node.id === post.id)
return (
<Layout
meta={post.frontmatter.meta || false}
title={post.frontmatter.title || false}
>
<SinglePostTemplate
{...post}
{...post.frontmatter}
body={post.html}
nextPostURL={_get(thisEdge, 'next.fields.slug')}
prevPostURL={_get(thisEdge, 'previous.fields.slug')}
/>
</Layout>
)
}
export default SinglePost
export const pageQuery = graphql`
## Query for SinglePost data
## Use GraphiQL interface (http://localhost:8000/___graphql)
## $id is processed via gatsby-node.js
## query name must be unique to this file
query SinglePost($id: String!) {
post: markdownRemark(id: { eq: $id }) {
...Meta
html
id
frontmatter {
title
template
subtitle
date(formatString: "MMMM Do, YYYY")
categories {
category
}
}
}
allPosts: allMarkdownRemark(
filter: { fields: { contentType: { eq: "posts" } } }
sort: { order: DESC, fields: [frontmatter___date] }
) {
edges {
node {
id
}
next {
fields {
slug
}
frontmatter {
title
}
}
previous {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`
【问题讨论】:
-
这能回答你的问题吗? react-router (v4) how to go back?
标签: reactjs react-router