创建React组件的npm模块时出错

用户913436

我正在尝试为我拥有的所有可重复使用的React组件创建一个节点模块。导入jsx文件时卡住了。我有一个基本的jsx模块,即components文件夹中的greeting.jsx。

//greeting.jsx
import React from 'react';
export default class Greeting extends React.Component {
    render() {
        return (
            <p>Hello World</p>
        )
    }
}

文件夹结构:-

- index.js
- components
  ¦-- Greeting
      ¦-- greeting.jsx
  ¦-- <Other Modules like Greeting>

index.js导入所有组件并将其导出

//index.js

import Greeting from './components/Greeting/greeting.jsx';

export default {
    Greeting
};

当我必须使用问候语模块时,我必须导入该模块。就像下面的代码一样。但是这样做给我页面上的错误

import React from 'react';
import ReactDOM from 'react-dom';
import GreetingModule from './index.js';

ReactDOM.render( <GreetingModule />, document.getElementById('content') );

错误:

warning.js:45 Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).
invariant.js:39 Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

这就是我现在被困住的地方。虽然如果我直接导​​入jsx文件(如下所示),则它可以工作。

import React from 'react';
import ReactDOM from 'react-dom';
import GreetingModule from './components/Greeting/greeting.jsx';

ReactDOM.render( <GreetingModule />, document.getElementById('content') );

但这不是我要创建的npm模块的方式,我的index.js应该导出所有react组件。

我曾尝试使用Google搜索来为反应组件创建一个npm模块,但找不到任何东西。请帮助,解决问题

艾米

问题是您正在隔离地在模块上运行babel转换。

我敢打赌,您的index.js事后转换看起来像:

var Greeting = require('./components/Greeting/greeting.jsx');

exports.default = {
  Greeting
};

问题就在这里。您的模块正在导出该default属性下的所有肉类因此,使用您的模块的人需要按以下方式使用它:

var Greeting = require('greeting').default;

您可以接受这种方式,也可以使用旧的在index.js中导出模块的方式。因此,您只需将index.js更改为:

//index.js

import Greeting from './components/Greeting/greeting.jsx';

module.exports = {
  Greeting
};

这应该够了吧。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Docker:在创建 dockerfile npm 包模块时出错

来自分类Dev

在react-native中导入npm模块时出错

来自分类Dev

在创建模块时出错

来自分类Dev

REACT - 呈现组件时出错

来自分类Dev

在React Native中使用npm install安装节点模块时出错

来自分类Dev

使用mysqldb模块创建表时出错

来自分类Dev

使用npm安装node.js模块时出错

来自分类Dev

使用npm安装node.js模块时出错

来自分类Dev

在Linux上安装NPM模块(打印机)时出错

来自分类Dev

将样式组件导入 React 组件时出错

来自分类Dev

使用cloudscraper模块创建.exe(pyinstaller)时出错[Python]

来自分类Dev

在复合组件内创建DataScroller时出错

来自分类Dev

Typscript / React扩展默认功能组件道具时出错

来自分类Dev

在React功能组件中使用useEffect时出错

来自分类Dev

尝试在Laravel视图上使用React组件时出错

来自分类Dev

呈现无状态组件时出错:“找不到变量:React”

来自分类Dev

启动npm时出错?

来自分类Dev

运行 npm 时出错

来自分类Dev

尝试导入npm时出错,进入create-react-app

来自分类Dev

尝试导入npm时出错,进入create-react-app

来自分类Dev

使用 npm 安装 react-native-gesture-handler 时出错

来自分类Dev

使用npm安装create-react-app时出错,使用npm init react-app时出错

来自分类Dev

自动声明组件,在创建新组件时路由模块中的路径 Angular 7

来自分类Dev

单元测试在测试 spring 集成 TCP 组件时创建名为“amqAdmin”的 bean 时出错

来自分类Dev

尝试使用React Native创建项目时出错

来自分类Dev

当将reactjs组件作为节点模块导出到npm时,我是否包含jsx或js

来自分类Dev

当模块被“ npm link”设置时,Vue 3组件未正确初始化

来自分类Dev

加载模块枕头时出错

来自分类Dev

.dll模块注册时出错

Related 相关文章

  1. 1

    Docker:在创建 dockerfile npm 包模块时出错

  2. 2

    在react-native中导入npm模块时出错

  3. 3

    在创建模块时出错

  4. 4

    REACT - 呈现组件时出错

  5. 5

    在React Native中使用npm install安装节点模块时出错

  6. 6

    使用mysqldb模块创建表时出错

  7. 7

    使用npm安装node.js模块时出错

  8. 8

    使用npm安装node.js模块时出错

  9. 9

    在Linux上安装NPM模块(打印机)时出错

  10. 10

    将样式组件导入 React 组件时出错

  11. 11

    使用cloudscraper模块创建.exe(pyinstaller)时出错[Python]

  12. 12

    在复合组件内创建DataScroller时出错

  13. 13

    Typscript / React扩展默认功能组件道具时出错

  14. 14

    在React功能组件中使用useEffect时出错

  15. 15

    尝试在Laravel视图上使用React组件时出错

  16. 16

    呈现无状态组件时出错:“找不到变量:React”

  17. 17

    启动npm时出错?

  18. 18

    运行 npm 时出错

  19. 19

    尝试导入npm时出错,进入create-react-app

  20. 20

    尝试导入npm时出错,进入create-react-app

  21. 21

    使用 npm 安装 react-native-gesture-handler 时出错

  22. 22

    使用npm安装create-react-app时出错,使用npm init react-app时出错

  23. 23

    自动声明组件,在创建新组件时路由模块中的路径 Angular 7

  24. 24

    单元测试在测试 spring 集成 TCP 组件时创建名为“amqAdmin”的 bean 时出错

  25. 25

    尝试使用React Native创建项目时出错

  26. 26

    当将reactjs组件作为节点模块导出到npm时,我是否包含jsx或js

  27. 27

    当模块被“ npm link”设置时,Vue 3组件未正确初始化

  28. 28

    加载模块枕头时出错

  29. 29

    .dll模块注册时出错

热门标签

归档