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 function useReferentialCallback<F extends (...args: any[]) => any>( | |
| fn: F, | |
| deps?: any[], | |
| ): F { | |
| const fnRef = React.useRef<F>(fn); | |
| React.useLayoutEffect(() => { | |
| fnRef.current = fn; | |
| }, deps); // eslint-disable-line |
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, { Dispatch, SetStateAction } from "react"; | |
| type ViolationMessage = string | (() => string); | |
| function useStableValue<V>( | |
| value: V, | |
| message: ViolationMessage = "Violation: cannot change value between re-renders", | |
| ) { | |
| const ref = React.useRef(value); |
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 { useMemo } from "react"; | |
| import { memoize } from "lodash"; | |
| /** | |
| * @description useful for callback that are passed to repeated component | |
| * @example | |
| * const makeRemove=useMemoizedCallback((date:Date)=>()=>{},String,[]); | |
| * ... | |
| * <ol>calendarEvents.map(calendarEvent => <li onClick={makeRemove(calendarEvent)}>{calendarEvent.toLocaleString()}</li>)</ol> | |
| */ |
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"; | |
| /** | |
| * Matches mediaquery, rerenders only when match changes | |
| * @param query media query | |
| */ | |
| export function useMediaQuery( | |
| query: string | null | false, | |
| defValue: boolean = 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
| /** | |
| * This is an implementation of javascript's imperative setInterval API | |
| * combined with React hooks' declarative approach. | |
| * | |
| * It is actually a replica of Dan Abramov's implementation described here: | |
| * https://overreacted.io/making-setinterval-declarative-with-react-hooks/ | |
| */ | |
| import { useEffect, useRef } from "react"; |
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, useEffect } from "react"; | |
| /** @description hook: returns true if given timestamp is in the future */ | |
| export function useExpiration( | |
| timestamp: number | null | undefined, | |
| ): boolean { | |
| const visible = timestamp != null && Date.now() <= timestamp; | |
| const [, forceRerender] = useState(0); | |
| useEffect(() => { | |
| if (visible && timestamp != null) { |
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 { useRef } from "react"; | |
| /** | |
| * @description used for identify changes between re-renders | |
| * @example | |
| * const changedProps = useChanges(props) | |
| * @example | |
| * useChanges(props, changes => console.log(changes)) | |
| */ | |
| export function useDebugPropsChanges<T extends Record<any, any>>( |
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 useDebounce<T>(value: T, delay: number) { | |
| const [debouncedValue, setDebouncedValue] = React.useState(value); | |
| React.useEffect(() => { | |
| const timeout = setTimeout(() => { | |
| setDebouncedValue(value); | |
| }, delay); |
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 useUndoReducer<State, Action>( | |
| reducer: (state: State, action: Action) => State, | |
| initial: () => State[] | |
| ) { | |
| const [index, setIndex] = useState(0); | |
| const [history, setHistory] = useState(initial); | |
| const state = history[index]; | |
| const dispatch = useCallback( | |
| (action: Action) => { | |
| const updated = history.slice(0, index + 1); |
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 useUndoState<T>(initial: T) { | |
| const [index, setIndex] = useState(0); | |
| const [history, setHistory] = useState([initial]); | |
| const state = history[index]; | |
| const setState = useCallback( | |
| (update: (s: T) => T) => { | |
| const updated = history.slice(0, index + 1); | |
| updated.push(update(state)); | |
| setHistory(updated); | |
| setIndex(index + 1); |