Next.js + React 返回上一页 [英] Next.js + React Go back to the previous page

查看:118
本文介绍了Next.js + React 返回上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用路由 next.js.如何实现按钮返回并返回上一页?就像浏览器上的按钮一样

I use routing next.js. How to implement the button back and return to the previous page? Just like with the button on the browser

推荐答案

EDIT:在这个答案首次发布 2 年后,路由器方法现在是 有据可查.这是直接来自文档的代码:

EDIT: 2 years after this answer was first posted, the router method is now well documented. Here is the code straight from the documentation:

import { useRouter } from 'next/router'

export default function Page() {
  const router = useRouter()

  return <span onClick={() => router.back()}>Click here to go back</span>
}

原始答案:

有一个未记录的 Router.back 方法(见源代码) 只执行 window.history.back()

There is an undocumented Router.back method (see source) that just does window.history.back()

你应该有一个像这样的组件

You should have a component like so

import Router from 'next/router'

export default function BackButton() {
    return (
        <div onClick={() => Router.back()}>Go Back</div>
    )
}

这篇关于Next.js + React 返回上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆