尝试时出现错误
import * as React from "react";
import { render } from "react-dom";
import "./styles.css";
const Person: React.FC = ({ name }) => <div>{name}</div>;
function App() {
return (
<div className="App">
<Person name="james" />
</div>
);
}
const rootElement = document.getElementById("root");
render(<App />, rootElement);
第4行有什么问题?
答案 0 :(得分:0)
您的问题不是破坏性,而是您必须为组件的props提供类型定义。一种方法是创建一个Props
类型,并将其作为泛型传递给React.FC
:
type Props = {
name: string;
};
const Person: React.FC<Props> = ({ name }) => <div>{name}</div>;