简体   繁体   English

Reacjs / TypeScript:自定义 HTMLATTRIBUTES => 声明为 INT 但在 DOM 中显示为“STRING”

[英]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 属性都用引号括" ,包括像heightwidth这样的数值。这意味着 HTML 代码中的numberstring之间没有区别。如果你想从e.currentTarget中提取值,你有使用parseInt将其转换为自己的number

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于字符串长度的 TypeScript 自定义类型检查 - TypeScript custom typecheck for string length 在 Typescript 中使用自定义 JSX pragma 的 DOM 元素的动态道具 - Dynamic props for DOM elements using custom JSX pragma in Typescript 带有 typescript 的路由器道具和自定义道具为功能组件反应路由器 dom - router props and custom props with typescript react router dom for functional components 打字稿:类型“字符串”不可分配给自定义类型 - Typescript: Type 'string' is not assignable to custom type 与 Typescript 反应:“DetailedHTMLProps”类型上不存在属性“名称” <HTMLAttributes<HTMLDivElement> , HTMLDivElement&gt;&#39; - React with Typescript: Property 'name' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>' 如何在不创建巨大的道具表的情况下使用 Storybook Docs + Typescript 扩展 HTMLAttributes? - How to extend HTMLAttributes with Storybook Docs + Typescript without creating a HUGE props table? 如何将 html 属性传递给孩子<img>元素使用 React 和 Typescript 没有“IntrinsicAttributes &amp; HTMLAttributes<htmlimageelement> 错误”?</htmlimageelement> - How to pass html attributes to child <img> element using React and Typescript without "IntrinsicAttributes & HTMLAttributes<HTMLImageElement> error"? ReactJS与TypeScript无法呈现给DOM - ReactJS with TypeScript not rendering to the DOM 从 TypeScript 中的 react-router-dom 访问自定义组件 props 和 URL 参数 - Access custom component props and URL parameters from react-router-dom in TypeScript 如何从库向React的HTMLAttributes添加自定义属性? - How can I add a custom attribute to React's HTMLAttributes from a library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM