This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCustomError extends Error { | |
customField?: string | |
constructor(msg: string, customData: Record<string, string>) { | |
super(msg) | |
this.customField = customData.customField | |
} | |
} | |
try { | |
throw new MyCustomError('Custom Error', { customField: 'additional data' }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/params.jsx | |
import Router, { useRouter } from 'lib/router' | |
function MyComponent() { | |
const { query } = useRouter() | |
function handleSubmit(evt) { | |
const params = new FormData(evt.currentTarget).getAll("fruits"); | |
evt.preventDefault(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lib/router.js | |
import NextRouter from 'next/router' | |
function push(url, opts) { | |
return NextRouter.push(url, null, opts) | |
} | |
const Router = { | |
...NextRouter, | |
push, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/params.jsx | |
import Router from 'next/router' | |
import { useRouter } from 'lib/router' | |
function MyComponent() { | |
const { query, pathname } = useRouter() | |
function handleSubmit(evt) { | |
const params = new FormData(evt.currentTarget).getAll("fruits"); | |
evt.preventDefault(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lib/router.js | |
import { useRouter as useNextRouter } from 'next/router' | |
export function useRouter() { | |
const router = useNextRouter() | |
const [pathname, queryString = ''] = router.asPath.split('?') | |
return Object.assign(router, { pathname, queryString }) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/params.jsx | |
import Router from 'next/router' | |
function MyComponent() { | |
const handleClick = useCallback(() => { | |
// some logic | |
Router.push('/profile') | |
}, []) | |
useEffect(() => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/params.jsx | |
import { useEffect, useCallback } from 'react' | |
import { useRouter } from 'next/router' | |
function MyComponent() { | |
const { push } = useRouter() | |
const handleClick = useCallback(() => { | |
// some logic | |
push('/profile') | |
}, [push]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Link from 'next/link' | |
// Before | |
function MyComponent() { | |
return ( | |
<Link href="/posts/[post]" as="/posts/blog-post"> | |
Blog post | |
</Link> | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Redirect | |
export function getServerSideProps() { | |
return { | |
redirect: { | |
destination: '/', | |
permanent: false, | |
}, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function fetchUser(params) { | |
const res = await fetch('https://example.com/api/user', params); | |
const data = await res.json(); | |
return data; | |
} | |
async function fetchUserFromServer(ctx) { | |
const { token } = ctx.req.cookies; | |
const user = await fetchUser({ headers: { Authorization: `Bearer ${token}` } }); |
NewerOlder