问题描述
我正在学习 TypeScript,有些地方让我感到困惑.下面是一点:
interface Props {名称:字符串;}const PrintName: React.FC<Props>=(道具)=>{返回 (<p style={{ fontWeight: props.priority ?大胆":正常"}}>{props.name}</p></div>)}const PrintName2 = (props: Props) =>{返回 (<p style={{ fontWeight: props.priority ?大胆":正常"}}>{props.name}</p></div>)}对于上面的两个功能组件,我看到 TypeScript 生成了相同的 JS 代码.就可读性而言,PrintName2 组件对我来说似乎更加精简.我想知道这两种定义有什么区别,是否有人使用第二种类型的 React 组件?
解决方案 感谢大家的回答.他们是正确的,但我正在寻找更详细的版本.我做了更多的研究,并在 React+TypeScript Cheatsheets 上找到了这个在 GitHub 上.
功能组件
这些可以写成普通函数,接受一个 props 参数并返回一个 JSX 元素.
type AppProps = { message: string };/* 也可以使用接口 */const App = ({ message }: AppProps) =><div>{消息}</div>;
React.FC/React.FunctionComponent 呢?您还可以使用 React.FunctionComponent(或简写 React.FC)编写组件:
const App: React.FC<{ message: string }>= ({ 消息 }) =>(<div>{消息}</div>);
与正常功能"的一些区别;版本:
它为 displayName、propTypes 和 defaultProps 等静态属性提供类型检查和自动完成功能 - 但是,目前存在使用 的已知问题>defaultProps 与 React.FunctionComponent.有关详细信息,请参阅此 问题 - 向下滚动到我们的 defaultProps 部分,用于在此处输入建议.
它提供了子项的隐式定义(见下文) - 但是隐式子项类型存在一些问题(例如,DefinitelyTyped#33006),无论如何,明确使用子项的组件可能被认为是一种更好的样式.
const Title: React.FunctionComponent<{ title: string }>= ({孩子们,标题}) =><div title={title}>{children}</div>;
将来,它可能会自动将 props 标记为只读,但如果 props 对象在参数列表中被解构,那将是一个有争议的问题.
React.FunctionComponent 是明确的返回类型,而普通函数版本是隐式的(或者需要额外的注释).
<块引用>在大多数情况下,使用哪种语法几乎没有区别,但是 React.FC 语法稍微有点冗长,没有提供明显的优势,因此优先考虑正常功能";语法.
I am learning TypeScript and some bits are confusing to me. One bit is below:
interface Props {
name: string;
}
const PrintName: React.FC<Props> = (props) => {
return (
<div>
<p style={{ fontWeight: props.priority ? "bold" : "normal" }}>
{props.name}
</p>
</div>
)
}
const PrintName2 = (props: Props) => {
return (
<div>
<p style={{ fontWeight: props.priority ? "bold" : "normal" }}>
{props.name}
</p>
</div>
)
}
For both functional components above, I see TypeScript generates the same JS code. The PrintName2 component seems more streamlined to me as far as readability. I wonder what's the difference between the two definitions and if anyone is using second type of React component?
解决方案 Thanks all for the answers. They are correct but I was looking for a more detailed version. I did some more research and found this on React+TypeScript Cheatsheets on GitHub.
Function Components
These can be written as normal functions that take a props argument and return a JSX element.
type AppProps = { message: string }; /* could also use interface */
const App = ({ message }: AppProps) => <div>{message}</div>;
What about React.FC/React.FunctionComponent?
You can also write components with React.FunctionComponent (or the shorthand React.FC):
const App: React.FC<{ message: string }> = ({ message }) => (
<div>{message}</div>
);
Some differences from the "normal function" version:
It provides typechecking and autocomplete for static properties like displayName, propTypes, and defaultProps - However, there are currently known issues using defaultProps with React.FunctionComponent. See this issue for details - scroll down to our defaultProps section for typing recommendations there.
It provides an implicit definition of children (see below) - however there are some issues with the implicit children type (e.g. DefinitelyTyped#33006), and it might be considered a better style to be explicit about components that consume children, anyway.
const Title: React.FunctionComponent<{ title: string }> = ({
children,
title
}) => <div title={title}>{children}</div>;
In the future, it may automatically mark props as readonly, though that's a moot point if the props object is destructured in the parameter list.
React.FunctionComponent is explicit about the return type, while the normal function version is implicit (or else needs additional annotation).
In most cases, it makes very little difference which syntax is used,
but the React.FC syntax is slightly more verbose without providing
clear advantage, so precedence was given to the "normal function"
syntax.
这篇关于TypeScript React.FC<Props>困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17
前端开发问题
437
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11
前端开发问题
455
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20
前端开发问题
5
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20
前端开发问题
5
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)