In TypeScript, u can easily declare constants using as const:
const Meal = {
Breakfast: 'breakfast',
Lunch: 'lunch',
Dinner: 'dinner'
}| import { groupBy } from "es-toolkit"; | |
| import { useCallback, useMemo, useState } from "react"; | |
| type SelectAllControl = { | |
| disabled: boolean; | |
| checkedState: boolean | "indeterminate"; | |
| onToggleSelect: () => void; | |
| }; | |
| type ItemSelectControl<T> = { |
| import { debounce } from "es-toolkit"; | |
| import { | |
| useCallback, | |
| useRef, | |
| useState, | |
| type ChangeEventHandler, | |
| type InputHTMLAttributes, | |
| } from "react"; | |
| import { useUpdateEffect } from "react-use"; |
| // why this won't work: | |
| // 1. ref is scoped to component | |
| // 2. there's no way to know whether the window is already opened | |
| import { useMemo, useRef } from "react"; | |
| export type WindowConfig = { | |
| /** window path */ | |
| path: string; |
| export const fruitOptions = [ | |
| { | |
| value: "apple", | |
| label: "Apple 🍎", | |
| }, | |
| { | |
| value: "grape", | |
| label: "Grape 🍇", | |
| }, | |
| { |
| form.addEventListener('submit', async (e) => { | |
| e.preventDefault(); | |
| const formData = new FormData(form); | |
| const response = await fetch(form.action, { | |
| method: form.method, | |
| body: formData | |
| }); | |
| // handle response... | |
| }); |
| import { PauseIcon, PlayIcon } from "Todo"; | |
| import { Button } from "Todo"; | |
| import * as Slider from "@radix-ui/react-slider"; | |
| import classNames from "classnames"; | |
| import throttle from "lodash.throttle"; | |
| import { | |
| useCallback, | |
| useEffect, | |
| useRef, | |
| useState, |
| import { useQuery } from "@tanstack/react-query"; | |
| import { useRef, useState } from "react"; | |
| async function fetchVideoContentLength(videoUrl: string) { | |
| const controller = new AbortController(); | |
| const signal = controller.signal; | |
| try { | |
| const response = await fetch(videoUrl, { | |
| // the idiomatic way of requesting only the response headers(without the content) is using a HEAD request, but server may not support the HEAD method | |
| method: "GET", |
| 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(""); |