[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

posted @   Zhentiw  阅读(150)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-12-28 [Javascript] Create a Custom Iterator for Any Array
2019-12-28 [Javascript] Create Your First Iterator in JavaScript
2018-12-28 [PWA] Cache JSON Data in a React PWA with Workbox, and Display it while Offline
2018-12-28 [PWA] Cache Third Party Resources from a CDN in a React PWA
2018-12-28 [React] Override webpack config for create-react-app without ejection
2018-12-28 [Algorithms] Classify Mystery Items with the K-Nearest Neighbors Algorithm in JavaScript
2018-12-28 [Angular] ViewChild 'read' option
点击右上角即可分享
微信分享提示