• [React Typescript 2022] Type React hooks


    Type useMemo:

        let rowArray = React.useMemo<null[]>(
            () => Array(board.rows).fill(null),
            [board.rows]
        );

    Type useCallback:

        let getColumnArray = React.useCallback(
            (rowIndex: number): Cell[] =>
                cells.slice(
                    board.columns * rowIndex,
                    board.columns * rowIndex + board.columns
                ),
            [board.columns, cells]
        );

    Type useRef:

    let ref = React.useRef<HTMLButtonElement>(null);
    
    <Button ref={ref} ... />

    Type useState:

    let [timeElapsed, setTimeElapsed] = React.useState<number>(0);

    Type Custom hook:

    function useTimer(gameState): [number, () => void] {
        let [timeElapsed, setTimeElapsed] = React.useState<number>(0);
        React.useEffect(() => {
            if (gameState === "active") {
                let id = window.setInterval(() => {
                    setTimeElapsed((t) => (t <= 999 ? ++t : t));
                }, 1000);
                return () => {
                    window.clearInterval(id);
                };
            }
        }, [gameState]);
        const reset = React.useCallback(() => {
            setTimeElapsed(0);
        }, []);
        return [timeElapsed, reset];
    }

    Type useReducer:

    The best way to type a useReducer is typing `reducer` function itself.

    let [{ gameState, cells, mines }, send] = React.useReducer(
            reducer,
            initialContext,
            function getInitialContext(ctx) {
                return {
                    ...ctx,
                    cells: createCells(board),
                };
            }
        );
    interface BoardContext {
        gameState: GameState;
        cells: Cell[];
        mines: number[];
        initialized: boolean;
    }
    type BoardEvent =
        | { type: "RESET"; board: BoardConfig }
        | { type: "REVEAL_CELL"; board: BoardConfig; index: number }
        | { type: "REVEAL_ADJACENT_CELLS"; board: BoardConfig; index: number }
        | { type: "MARK_CELL"; index: number }
        | { type: "MARK_REMAINING_MINES"; board: BoardConfig };
    function reducer(context: BoardContext, event: BoardEvent): BoardContext { ... }
  • 相关阅读:
    【转】Exchange Server 的防火墙开放端口
    【转】AD常用端口
    centos6安装python3
    CMD下的netstat命令
    CMD定时倒数
    【转】netstat 的10个基本用法
    不联网如何PING通WIN主机和VMWARE(懵懂版,不含原理)
    【转载】Putty出现 Network error:Software caused connection abort
    MFC Unicode 字符集下格式转换
    MFC嵌套OpenCV窗口并显示图片
  • 原文地址:https://www.cnblogs.com/Answer1215/p/15745272.html
Copyright © 2020-2023  润新知