In TypeScript, u can easily declare constants using as const
:
const Meal = {
Breakfast: 'breakfast',
Lunch: 'lunch',
Dinner: 'dinner'
}
export function getErrorMessage(err: unknown, defaultMessage = "An error occurred") { | |
if (err instanceof Error && err.message) { | |
return err.message; | |
} | |
if (err instanceof Object && "message" in err && typeof err.message === "string" && err.message) { | |
return err.message; | |
} | |
if (typeof err === "string" && err) { | |
return err; | |
} |
In TypeScript, u can easily declare constants using as const
:
const Meal = {
Breakfast: 'breakfast',
Lunch: 'lunch',
Dinner: 'dinner'
}
import { useState } from "react"; | |
import { useBeforeUnload, useBlocker } from "react-router-dom"; | |
// unstable_usePrompt: https://reactrouter.com/en/main/hooks/use-prompt | |
// useBlocker: https://reactrouter.com/en/main/hooks/use-blocker | |
// useBeforeUnload: https://reactrouter.com/en/main/hooks/use-before-unload | |
export default function Form() { | |
const [input, setInput] = useState(""); |
/** | |
* 以下代码来自 react-router-dom v6,用于解决 react-router-dom v5 没有 useSearchParams 的问题 | |
* see: https://github.com/remix-run/react-router/blob/main/packages/react-router-dom/index.tsx#L1432 | |
* 在原代码的基础上做的改动: | |
* 1. 原代码使用了 useNavigate,但 react-router v5 还没有这个 hook,使用 useHistory 代替(https://reactrouter.com/en/main/upgrading/v5#use-usenavigate-instead-of-usehistory) | |
* 2. 在返回的数组中提供 updateSearchParams 以在保留其他参数的情况下设置/更新指定的参数 | |
*/ | |
import React, { useCallback } from 'react'; | |
import { useLocation, useHistory } from 'react-router'; |
const list = [ | |
{ province: '天津', city: '南开', district: '三马路' }, | |
{ | |
province: '天津', | |
city: '南开', | |
district: '四马路' | |
}, | |
{ | |
province: '天津', | |
city: '河西', |
// this script does not handle file suffix for you | |
// as some files have multiple suffix segments, e.g: *.sc.ass & *.tc.ass are two common subtitle formats | |
// instead, you should manually specify suffix in regex as the last capture group | |
const fs = require("fs") | |
const isDryrun = !process.argv.includes("-r"); | |
if (isDryrun) { | |
console.log("Note: By default, this script will only dry run, specify -r if you want to actually run it.") |
<section className="h-full w-full flex justify-center items-center gap-8 p-6 flex-col md:flex-row"> | |
<div className="rounded-full w-36 h-36 bg-black font-semibold flex justify-center items-center text-6xl text-white"> | |
404 | |
</div> | |
<div className="flex flex-col items-end"> | |
<div className="text-5xl">Oops</div> | |
<div className="text-2xl mt-2 mb-6 text-end">This page doesn't exist</div> | |
<span className="bg-black text-white px-4 py-2 rounded-md cursor-pointer"> | |
Home | |
</span> |
import { FallbackProps } from "react-error-boundary"; | |
import React, { CSSProperties } from "react"; | |
const ErrorFallback: React.ComponentType<FallbackProps> = ({ | |
error, | |
resetErrorBoundary, | |
}) => { | |
const sectionStyle: CSSProperties = { | |
display: "flex", | |
flexDirection: "column", |
function check() { | |
const minimalUserResponseInMiliseconds = 200; | |
console.clear(); | |
let before = new Date().getTime(); | |
debugger; | |
let after = new Date().getTime(); | |
if (after - before > minimalUserResponseInMiliseconds) { | |
document.write(" Don't open Developer Tools."); | |
window.location.reload(); | |
} |