元素类型无效:需要字符串(对于内置组件)还是类/函数?

时间:2019-07-27 20:48:37

标签: reactjs storybook

我遇到了一个奇怪的React / Storybook错误。

我看到的错误是:

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.

以下是导致错误的代码:

import React from 'react';
import styled from 'styled-components';

class LogoWordMark extends React.PureComponent {
  static Container = styled.div``;

  render() {
    return (
      <LogoWordMark.Container>
        mnkladjkl asdjklas djkkjlsad jklasdjklasd kl
        <a>asdad</a>
      </LogoWordMark.Container>
    );
  }
}

export default LogoWordMark;

奇怪的是,如果我用以下内容更新一行:

    <a onClick={this.blah}>asdad</a>

然后,该组件将按预期方式呈现。即使未定义this.blah。有人知道这里发生了什么吗?谢谢

我正在使用:

"react": "16.8.6",
"@storybook/react": "5.1.7",

2 个答案:

答案 0 :(得分:1)

基于full example you posted

您默认将class Foo(): def __lt__(self, other): if isinstance(other, int): return self._id < other elif isinstance(other, self.__class__): return self._id < other._id def __gt__(self, other): if isinstance(other, int): return self._id > other elif isinstance(other, self.__class__): return self._id > other._id 导出到那里,而不导出importing it as one。将Brand更改为import { Brand } from "./Brand";it should work.


这是错误最后试图警告的地方:

  

您可能混淆了默认导入和命名导入。

答案 1 :(得分:0)

我的所有导出和导入都是正确的,因此@SamVK 的回答不适用于我收到此错误的原因。

当使用相对于另一个组件是静态的组件时,似乎会发生此错误。我必须做的修复是重新启动我的开发服务器 (NextJS)。 (灵感来自 SamKV 对原始 OP 的评论回复)