• [React Typescript 2022] Refactor a React Component using TypeScript


    We are going to start refactoring our CountDisplay component. It is a small stateless component but it has a few props that can benefit from type safety.

    There are three ways to type a component, inline, alias, and as a function expression. The inline typing adds a bit of noise to our code and can make it difficult to parse right out of the gate. To fix this, we use a type alias that reads a little bit nicer. To add in a function expression, which we get from the React Types that we downloaded, we can declare this variable to have a type of React.FunctionComponent which takes as a type argument, our props.

    JS:

    import * as React from "react";
    import cx from "clsx";
    import { scope } from "../lib/utils";
    
    function CountDisplay({ count, className }) {
        let countString = String(Math.max(Math.min(count, 999), -99));
        return (
            <div className={cx(scope("count-display"), className)}>{countString}</div>
        );
    }
    
    export { CountDisplay };

    TS:

    import * as React from "react";
    import cx from "clsx";
    import { scope } from "../lib/utils";
    
    function CountDisplay({ count, className }: CountDisplayProps) {
        let countString = String(Math.max(Math.min(count, 999), -99));
        return (
            <div className={cx(scope("count-display"), className)}>{countString}</div>
        );
    }
    
    export { CountDisplay };
    
    interface CountDisplayProps {
        count: number;
        className?: string;
    }

    Or:

    For function component:

    TS:

    import * as React from "react";
    import cx from "clsx";
    import { scope } from "../lib/utils";
    
    const CountDisplay: React.FunctionComponent<CountDisplayProps> = ({
        count,
        className,
    }) => {
        let countString = String(Math.max(Math.min(count, 999), -99));
        return (
            <div className={cx(scope("count-display"), className)}>{countString}</div>
        );
    };

    CountDisplay.displayName = "Count"; export { CountDisplay }; interface CountDisplayProps { count: number; className
    ?: string; }

    or a Shorter way:

    const CountDisplay: React.FC<CountDisplayProps> = ({
        count,
        className,
    }) => {
        let countString = String(Math.max(Math.min(count, 999), -99));
        return (
            <div className={cx(scope("count-display"), className)}>{countString}</div>
        );
    };

    FC<> always imply children, So if your component doesn't accept Children, you can use `VFC` or `VoidFunctionComponent`.

    const CountDisplay: React.VFC<CountDisplayProps> = ({
        count,
        className,
    }) => {
        let countString = String(Math.max(Math.min(count, 999), -99));
        return (
            <div className={cx(scope("count-display"), className)}>{countString}</div>
        );
    };

    Read more 

    TypeScript + React: Why I don't use React.FC

  • 相关阅读:
    scikit_learn 官方文档翻译(集成学习)
    机器学习之SVM与逻辑回归的联系和区别
    有序数组寻找中位数以及寻找K大元素
    有向图算法之拓扑排序
    机器学习之离散型特征处理--独热码(one_hot_encoding)
    计算广告学(2)--广告有效性模型
    机器学习实战--k-均值聚类
    SonarQube 扫描代码,SonarQube 进行代码质量检查
    Docker 搭建 Nexus3
    informix 安装 linux 客户端
  • 原文地址:https://www.cnblogs.com/Answer1215/p/15742796.html
Copyright © 2020-2023  润新知