- vừa được xem lúc

Handling custom page navigation on Next.js

0 0 71

Người đăng: Sirajus Salekin

Theo Viblo Asia

Last month we discussed about different kind of routing on Next.js. While next/link is enough for all of our routing requirements, we can also handle client side navigation without using it.

Here's a short example of creating a component that moves the user to next page. You need to import the useRouter component from next/router, then you can just router.push(url)

import { useRouter } from 'next/router' function NextPage() { const router = useRouter() return ( <span onClick={() => router.push('/some_different_page')}> Visit next page </span> )
} export default NextPage

The useRouter is a react hook, so by default it will only work with functional react components, and not work with class components. If you want similar behavior on class components, you need to use withRouter. Both useRouter and withRouter will return the router object, so everything else is similar.

you can read more about this here.

import { withRouter } from 'next/router' function Page({ router }) { return <span>{router.pathname}</span>
} export default withRouter(Page)

router.push

This is the core function for handling custom navigation of your next.js app. You use it like

router.push("some url")

You can also send some optional params

router.push("some url", as, options)
as

optional decorator for the URL that users will see. Prior to next v9.5 this was used for handling dynamic routes.

options

optional object you send as param. Inside the object, it takes shallow, getStaticProps, getInitialProps, getServerSideProps

Here's an example

router.push('/?counter=10', undefined, { shallow: true })

By default, the options you don't mention inside the object will be false, so in the above example getStaticProps, getInitialProps, getServerSideProps all will be set as false.

A good example is, redirecting the user to login page, after he requests for something that requires authentication.

import { useEffect } from 'react'
import { useRouter } from 'next/router' // Here you would fetch and return the user
const useUser = () => ({ user: null, loading: false }) export default function Page() { const { user, loading } = useUser() const router = useRouter() useEffect(() => { if (!(user || loading)) { router.push('/login') } }, [user, loading]) return <p>Redirecting...</p>
}

One thing to note here, you don't need to use router.push when navigating to an external link. In those cases, just using the browser's window.location is enough.

router.replace

this is useful for when you don't want any entry on your history. Other than that, it's exactly similar to router.push

router.replace("some url you don't want in history", as, options)

router.reload

Reloads the current page, duh. It executes

window.location.reload()

router.back

Moves the user one page ago in history. It executes

window.history.back()

router.prefetch

Use prefetch to make your pages appear faster on production. (automatically handled when using next/link) You use it like

router.prefetch("some url", as) // as is optional like in router.push

Here's an excellent scenario from next.js doc.

Imagine a case where the user will see the dashbord after he logs in successfully. In this case, we can prefetch the dashboard, while the user is still in login page, thus make the experience snappier.

import { useCallback, useEffect } from 'react'
import { useRouter } from 'next/router' export default function Login() { const router = useRouter() const handleSubmit = useCallback((e) => { e.preventDefault() fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ /* Form data */ }), }).then((res) => { // Do a fast client-side transition to the already prefetched dashboard page if (res.ok) router.push('/dashboard') }) }, []) useEffect(() => { // Prefetch the dashboard page router.prefetch('/dashboard') }, []) return ( <form onSubmit={handleSubmit}> {/* Form fields */} <button type="submit">Login</button> </form> )
}

Learning Material

Obviously, the next.js/doc

Bình luận

Bài viết tương tự

- vừa được xem lúc

Viết một chiếc app quản lý Hạn sử dụng của thực phẩm

Mở đầu. Tôi là 1 kỹ sư công nghệ thông tin mới ra trường. Trong thời gian học Đại học, từ năm 1 tới năm 4, tôi đi làm thêm tại TSUTAYA (chuỗi cửa hàng bán sách, video...v.v nổi tiếng bên Nhật). Về chiếc App tôi đã phát triển. App tôi phát triển là Web App giúp quản lý hạn sử dụng của đồ ăn.

0 0 40

- vừa được xem lúc

Routing trong Next.js

Trước đó bạn có thể đang tò mò về Next.js, vào đây luôn bạn ơi. (go). .

1 0 44

- vừa được xem lúc

Cài đặt chuyển đổi ngôn ngữ trong một project NextJS

Ở phiên bản Nextjs Version 10 mới đây, việc sử dụng i18n đã trở nên dễ dàng hơn qua tính nâng cao là Internationalized Routing. Cài đặt cấu hình cơ bản. Tại file next.config.

0 0 55

- vừa được xem lúc

Cấu hình eslint airbnb, prettier cho dự next.js sử dụng typescript

Chào mọi, mình đã quay lại đây. Hôm nay mình sẽ đem đến một chủ đề linter cụ thể cấu hình eslint cho dự án next.

0 0 177

- vừa được xem lúc

Next Image một feature, một nâng cấp tuyệt vời version nextjs 10

NextJS đã cho ra mắt version 10 cách đây 2 tháng với hơn 20 fearture nâng cấp đáng giá. Chúng ta có thể kể ra những features nổi bật như. . Next/Image: với khẳ năng render image cho các kích thước màn hình tương ứng.

0 0 49

- vừa được xem lúc

Ứng dụng web kết hợp API Laravel và next.js từ a-Z (Phần 1- hiển thị dữ liệu)

1. Lời nói đầu:. Chào mọi người, chắc hẳn với lập trình viên chúng ta, API là một cụm từ rất quen thuộc với chúng ta. Thông thường, với những ứng dụng web lớn, chúng ta thường chia thành 2 thành phần là API và Web.

0 0 133