【发布时间】:2020-09-14 18:05:14
【问题描述】:
我开始看到我的一些团队编写了以下代码,这让我怀疑我们是否以正确的方式做事,因为我以前从未见过这样写。
import * as React from "react";
import "./styles.css";
function UsualExample() {
return <h1>This is the standard example…</h1>
}
export default function App() {
const CustomComponent = (): JSX.Element => <h1>But can I do this?</h1>
return (
<div className="App">
<UsualExample />
<CustomComponent />
</div>
);
}
它似乎渲染得很好,我看不到任何直接的不利影响,但是我们不应该在另一个组件中定义 CustomComponent 功能组件有什么根本原因吗?
代码沙盒示例: https://codesandbox.io/s/dreamy-mestorf-6lvtd?file=/src/App.tsx:0-342
【问题讨论】:
-
是的,你可以!不要永远这样做,这不合逻辑且毫无意义。
标签: javascript reactjs jsx react-functional-component