reactjs - 类型 'boolean' 不可分配给类型 'number'.ts
问题描述
这是我的代码
App.tsx
import React, { useState } from 'react';
import logo from './logo.svg';
import './App.css';
import { TextField } from './components/TextField';
function App() {
return (
<div className="App">
<TextField name='fef' age=12/>
</div>
);
}
export default App;
TextField.tsx
import React,{useEffect, useRef, useState} from "react";
interface Props{
name: string;
age: number;
}
export const TextField:React.FC<Props> = ({name,age}) => {
return (
<div>
<h2>Hello {name}, and I know that you are {age} years old</h2>
</div>
);
}
但是我收到了这个错误!
Type 'boolean' is not assignable to type 'number'.ts(2322)
TextField.tsx(5, 5): The expected type comes from property 'age' which is declared here on type 'IntrinsicAttributes & Props & { children?: ReactNode; }'
我应该怎么做?
请给我一个解决方案。我是 React 和 Typescript 的新手。
解决方案
数字作为道具应该在大括号内传递。只有字符串可以在没有大括号的情况下传递,但它们必须包含在引号内,就像你的name
道具一样。
return (
<div className="App">
<TextField name='fef' age={12}/>
</div>
);
推荐阅读
- git - GitHub 子模块跟踪最新的分支,没有“git 子模块更新”
- google-cloud-platform - 如何获取 GKE 节点池中的节点数作为堆栈驱动程序指标?
- asp.net-core - 在 asp.net core 2.2 mvc 应用程序中获取 AzureAD 刷新令牌
- c++ - C++中的递归函数和性能
- class - 如何获取有关 dart 中的类的信息?
- javascript - 无法从 CodeBehind 调用 JavaScript 函数
- amazon-web-services - AWS Elastic beanstalk 挂钩失败:- 无法将文件复制到 c:/windows/fonts
- excel - 一个单元格如何在 Google 表格/Excel 中具有 2 个公式条件?
- python - 计算时差并以小时为单位返回结果
- excel - VBA 如果和不等式运算不起作用