[英]React - How to Export Multiple Components on same File, preventing <Unknown/> Component on Dev Tools
okay so if I have one react component I can:好吧,如果我有一个反应组件,我可以:
const Home = () => (....)
export default Home;
Now I have a file called About.js
where I am exporting multiple components as they're related.现在我有一个名为About.js
的文件,我在其中导出多个相关的组件。
export const Staff = () => (...)
export const Employee = () => (...)
in my App.js在我的 App.js 中
import { Staff, Employee } from "./components/About";
All works, however, react dev-tools says the components above are <Unknown/>
然而,所有的工作,react dev-tools 说上面的组件是<Unknown/>
What is best practice to export multiples components from a file avoiding the <Unknown/>
output?从避免<Unknown/>
输出的文件中导出多个组件的最佳实践是什么?
Solution Thanks to @Khun on comments bellow解决方案感谢@Khun 对下面的评论
About.js
const Staff = () => (...)
const Employee = () => (...)
export {Staff, Employee}
in my App.js
import { Staff, Employee } from "./components/About";
That removed the <Unknown/>
from react dev tools.这从反应开发工具中删除了<Unknown/>
。
Ps: I understand the discrepancy on best practice, however, this did resolve my question above. Ps:我理解最佳实践的差异,但是,这确实解决了我上面的问题。
First, create each components in a different file: Staff.js and Employee.js.首先,在不同的文件中创建每个组件:Staff.js 和 Employee.js。 Export them like this: export { Staff };
像这样导出它们: export { Staff };
and export { Employee };
并export { Employee };
Now create another file to handle multiple exports.现在创建另一个文件来处理多个导出。 Let's call it About.js and here is the content:让我们称之为 About.js,这里是内容:
export * from './Staff';
export * from './Employee';
Now just use it as you did:现在就像你一样使用它:
import { Staff, Employee } from "./components/About";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.