gpt4 book ai didi

reactjs - 将数字类型作为 React 组件属性传递

转载 作者:行者123 更新时间:2023-12-04 00:05:43 26 4
gpt4 key购买 nike

我刚刚开始在 asp.net core 2.0(React 项目类型)上使用 React。我在将数字类型传递给我的组件时遇到了一些麻烦。例如,如果我尝试传递数字类型 (id),则会收到错误消息:

TS) 类型 '{id: boolean;}' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes...

import * as React from 'react';
interface HelloWorldProps{
name: string
id: number
}
export class Greeting extends React.Component<HelloWorldProps, any> {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
export default Greeting;

当我尝试渲染组件时,我在此处收到 TS 错误...
<HelloWorld id=1 />

没有 id 属性它工作正常。

最佳答案

您应该将数值括在花括号中,如下所示 -

<HelloWorld id={1} />

关于reactjs - 将数字类型作为 React 组件属性传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46437549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com