[英]What is wrong with this import export component pattern in React?
In my App's React Component library, I have a UIC called Branch which has: 在我的应用程序的React Component库中,我有一个名为Branch的UIC,它具有:
Brand.Logo
Brand.WordMark
Brand.WorkMarkLogo
Brand.Logo and Brand.WordMark work fine but Brand.WorkMarkLogo outputs the following error in storybook: Brand.Logo和Brand.WordMark可以正常工作,但Brand.WorkMarkLogo在故事书中输出以下错误:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Please see here: https://codesandbox.io/s/hmfnu 请在这里查看: https : //codesandbox.io/s/hmfnu
What am I doing wrong? 我究竟做错了什么?
In the export default pattern, curly braces are not used. 在导出默认模式中,不使用花括号。
import {Brand} from "./Brand";
correct code. 正确的代码。
import Brand from "./Brand";
Only in the 'export' pattern, curly braces are used. 仅在“导出”模式下,才使用花括号。
export const abc = () => {};
import {abc} from '...';
When you import and wrap them in curly braces, you're importing a named export
, not a default export
. 当将它们导入并用大括号括起来时,您将导入一个named export
而不是default export
。
In your brand/index.js
file, you have on line 10: 在brand/index.js
文件中,您具有第10行:
export default Brand;
To import a default export in the module, you'll have to omit the curly braces. 要在模块中导入默认导出,您必须省略花括号。
So in your root/index.js
file, change line 3 to 因此,在您的root/index.js
文件中,将第3行更改为
import Brand from "./Brand";
Alternatively, you can choose to NOT export a default in your brand/index.js
file, and change put the export in front of your class like this: 或者,您可以选择brand/index.js
文件中导出默认值,并像下面这样更改将导出内容放在类的前面:
export class Brand extends React.Component {
static Logo = Logo;
static LogoWordMark = LogoWordMark;
}
With this method, you can add additional classes like in the same brand/index.js
file by adding : 使用此方法,您可以通过添加,在同一brand/index.js
文件中添加其他类:
export class Brand2 extends React.Component {
static Logo2 = Logo2;
static LogoWordMark2 = LogoWordMark2;
}
Then you'll be able to import both named exports like: https://codesandbox.io/s/react-example-955rf 然后,您将能够导入两个命名的导出,例如: https : //codesandbox.io/s/react-example-955rf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.