[英]Reacjs / TypeScript : Custom HTMLATTRIBUTES => Declared as a INT but shown in the DOM as a “STRING”
sorry for my terrible English.对不起我糟糕的英语。
I am trying to build an app in ReactJS + Typesctript... and it is completly HELL.我正在尝试在 ReactJS + Typesctript 中构建一个应用程序......它完全是地狱。
In a component TABS
, I declared 2 customs HTMLATTRIBUTES:在一个组件TABS
中,我声明了 2 个自定义 HTMLATTRIBUTES:
declare module 'react' {
interface HTMLAttributes<T> extends AriaAttributes, DOMAttributes<T> {
**playerid?: number;**
component?: string;
}
};
The Tabs component is used like this logic: Tabs 组件的使用逻辑如下:
<Tabs>
{
tabList.map(tab => (
<Tab
component={tab.component}
**playerid={player.id}**
key={tab.id}
title={tab.label}
icon={tab.icon}
name={tab.name}
selected>
</Tab>
))
}
</Tabs>
The < Tab > Component has thoses params declared: <Tab> 组件声明了这些参数:
type Params = {
title: string;
children: React.ReactNode;
icon: string;
selected?: boolean;
name:string;
**playerid:number;**
component:string;
}
And finally i render in the < Tabs >最后我在 < Tabs > 中渲染
childrenArray.map(child => (
React.isValidElement(child) ?
<li
component={child.props.component}
playerid= {Number(child.props.playerid)}
className={`${child.props.name} ${((child.props.name === tabActive) && "is-active")?"is-active":""}`}
onClick = { (e: React.MouseEvent) => ChangeActiveTab(e)}
key={child.key}>
<a>
<span className="icon is-small"><i className={`fa ${child.props.icon}`}></i></span>
<span>{child.props.title}</span>
</a>
</li> : 0
))
When i check the code HTML on Chrone Inspector:当我在 Chrone Inspector 上检查代码 HTML 时:
<ul>
<li component="biography" **playerid="1"** class="tabBio is-active">
<a>
<span class="icon is-small"><i class="fa fa-book"></i></span>
<span>Biographie</span>
</a>
</li>
</ul>
PLAYERID is a String? PLAYERID 是一个字符串? why?为什么? how can I fix it ?我该如何解决?
Thanks for helping, I hope i was clear enought.感谢您的帮助,我希望我很清楚。
All HTML attributes are enclosed in quotes "
, including numeric values like height
and width
. This means that there is no distinction between a number
and a string
in the HTML code. If you want to extract the value from an e.currentTarget
, you have to cast it to a number
yourself using parseInt
.所有HTML 属性都用引号括"
,包括像height
和width
这样的数值。这意味着 HTML 代码中的number
和string
之间没有区别。如果你想从e.currentTarget
中提取值,你有使用parseInt
将其转换为自己的number
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.