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
| const COUNTRY_CODES = { | |
| "US": "United of States", | |
| "CA": "Canada", | |
| "GB": "United Kingodm", | |
| }; | |
| // type definition | |
| type CountryCode = keyof typeof COUNTRY_CODES; // equivalent to: type CountryCode = "US" | "CA" | "GB"; | |
| type CountryName = typeof COUNTRY_CODES[CountryCode]; // equivalent to: type CountryName = "United of States" | "Canada" | "United Kingdom"; |
How to extract the merged arguments of an array of functions? And also the merged results of those functions?
(Source from https://twitter.com/flybayer/status/1316844216199372801)
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 React from 'react' | |
| export default function CountdownTimer({ startSeconds }) { | |
| const [seconds, setSeconds] = React.useState(startSeconds); | |
| const timeoutCallback = React.useCallback(() => { | |
| if(seconds >= 1) | |
| setSeconds(seconds - 1); | |
| }, [seconds]); |
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
| export default function createCrudHooks({ | |
| baseKey, | |
| indexFn, | |
| singleFn, | |
| createFn, | |
| updateFn, | |
| deleteFn, | |
| }) { | |
| const useIndex = (config) => useQuery([baseKey], indexFn, config) | |
| const useSingle = (id, config) => |
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
| const stringLitArray = <L extends string>(arr: L[]) => arr; | |
| const linkTypes = stringLitArray(['a', 'link']); | |
| export type LinkType = (typeof linkTypes)[number]; | |
| const isLinkType = (x: any): x is LinkType => linkTypes.includes(x); | |
| let link = 'a'; | |
| if (isLinkType('a')) { | |
| console.log("The current link is of type 'LinkType'"); | |
| } |
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
| const matchDark = '(prefers-color-scheme: dark)' | |
| function useDarkMode() { | |
| const [isDark, setIsDark] = React.useState( | |
| () => window.matchMedia && window.matchMedia(matchDark).matches | |
| ); | |
| React.useEffect(() => { | |
| const matcher = window.matchMedia(matchDark); | |
| const onChange = ({ matches }) => setIsDark(matches); |
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
| function* search(node) { | |
| if (!node) return; | |
| yield node; | |
| yield* search(node.firstChild); | |
| yield* search(node.nextSibling); | |
| } | |
| // example usage | |
| for (let node of search(document)) { | |
| if (node.localName === 'title') { |