【问题标题】:React - getting error export Multiple components for use as an NPM packageReact - 得到错误导出多个组件用作 NPM 包
【发布时间】:2020-03-16 03:52:42
【问题描述】:

button.js

import React, { Component } from "react";

class ButtonElements extends Component {
  render() {
    return (
      <button type={this.props.btnType} onClick={this.props.click}>
        {this.props.btnText}
      </button>
    );
  }
}

export default ButtonElements;

文本.js

import React from "react";

const TextElement = props => <h2>{props.label}</h2>;

export default TextElement;

index.js

import ButtonElements from "./component/button/button";
import TextElement from "./component/text/text";

export { ButtonElements, TextElement };

webpack.config.js

module.exports = {
  mode: "production",
  entry: ["./src/index.js"],
  output: {
    path: path.resolve("lib"),
    filename: "index.js",
    libraryTarget: "commonjs2"
}

我的项目:

import ButtonElements from "npm-package";
import TextElement from "npm-package";

<ButtonElements iClass="faSort" btnText="T"></ButtonElements>
<TextElement label="T"></TextElement>

错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入

【问题讨论】:

  • 你试过import { ButtonElements } from "npm-package";吗?
  • 它工作正常。 ``` 从“npm-package”导入 { ButtonElements } ```

标签: reactjs npm import components export


【解决方案1】:

import { ButtonElements } from "npm-package";

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 2019-09-09
    • 2018-10-30
    • 2018-07-12
    • 2021-04-28
    • 2020-01-04
    • 2022-07-12
    • 2020-07-16
    • 2019-12-22
    相关资源
    最近更新 更多