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 response = [] | |
| const userObj = { | |
| id: "fd70a8cd-32b5-437a-98b6-2363bda276f1", | |
| name: "Antonio", | |
| lastName: "Galindo", | |
| cardId: 123456789 | |
| }; | |
| userObj[Symbol.iterator] = function* () { | |
| const values = Object.values(userObj); |
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 bgColor: Record<string, string> = { | |
| Warning: 'orange', | |
| Error: 'red', | |
| Success: 'green', | |
| } | |
| type msgType = keyof typeof bgColor; | |
| function createPrintLog(type: msgType) { | |
| return function log(str: string) { |
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
| type ErrorWithMessage = { | |
| message: string | |
| } | |
| function isErrorWithMessage(error: unknown): error is ErrorWithMessage { | |
| return ( | |
| typeof error === 'object' && | |
| error !== null && | |
| 'message' in error && | |
| typeof (error as Record<string, unknown>).message === 'string' |
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 { useEffect } from 'react'; | |
| export default function useDocumentTitle(title: string) { | |
| useEffect(() => { | |
| document.title = title; | |
| }, [title]); | |
| } |
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 { useState } from 'react'; | |
| export default function useDefault<T>(initialState: T, defaultState: T) { | |
| const [value, setValue] = useState<T | null | undefined>(initialState); | |
| if (value === null || typeof value === 'undefined') { | |
| return [defaultState, setValue]; | |
| } | |
| } |
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 { useState, useCallback } from 'react'; | |
| export default function useToggle(initialValue: boolean = true) { | |
| const [value, setValue] = useState(Boolean(initialValue)); | |
| const handleValue = useCallback( | |
| (newValue: any) => | |
| typeof newValue === 'boolean' | |
| ? setValue(newValue) | |
| : setValue((currentValue) => !currentValue), |
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 * as React from 'react'; | |
| export default function usePrevious(newValue: string) { | |
| const [value, setValue] = React.useState<string>(newValue); | |
| const [preValue, setPreValue] = React.useState<string | null>(null); | |
| if (newValue !== value) { | |
| setPreValue(value); | |
| setValue(newValue); | |
| } |
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 { useSyncExternalStore } from 'react'; | |
| const subcribe = (cb: any) => { | |
| addEventListener('languagechange', cb); | |
| return () => removeEventListener('languagechange', cb); | |
| }; | |
| const getSnapshot = () => { | |
| return navigator.language; | |
| }; |
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 * as React from 'react'; | |
| export default function useFavicon(faviconUrl: string) { | |
| React.useEffect(() => { | |
| let link = document.querySelector(`link[rel~="icon"]`); | |
| if (!link) { | |
| link = document.createElement('link'); | |
| link.type = 'image/x-icon'; |
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 * as React from 'react'; | |
| function oldSchoolCopy(text) { | |
| const tempTextArea = document.createElement('textarea'); | |
| tempTextArea.value = text; | |
| document.body.appendChild(tempTextArea); | |
| tempTextArea.select(); | |
| document.execCommand('copy'); | |
| document.body.removeChild(tempTextArea); | |
| } |
OlderNewer