[英]how can i use both class and functional component in same file
I want to use both class and function in the same file and I am getting a parsing error that我想在同一个文件中同时使用 class 和 function 并且我收到一个解析错误
App is already declared应用程序已声明
My code:我的代码:
import React from 'react'
import './App.css';
class App extends React.Component{
}
function App() {
return (
<div>
</div>
);
}
export default App;
How can I use both class and functional component in same file?如何在同一个文件中同时使用 class 和功能组件?
You cannot use a class component with a functional component inside of it.您不能使用内部带有功能组件的 class 组件。 Because they work different, for example.例如,因为它们的工作方式不同。 Inside a class based component you have the render method that returns an html.在基于 class 的组件中,您具有返回 html 的render方法。
import React, { Component } from "react";
class ClassComponent extends Component {
render() {
return <h1>Hello, world</h1>;
}
}
And in a functional component the return of the function is and html.在一个功能组件中,function 的返回是和 html。
import React from "react";
function FunctionalComponent() {
return <h1>Hello, world</h1>;
}
You can also declare a functional component through a anonymous function也可以通过匿名 function 声明一个功能组件
import React from "react";
const FunctionalComponent = () => {
return <h1>Hello, world</h1>;
};
For further information you can check here or the documentation有关更多信息,您可以查看 此处或文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.