将一个react组件发布到npm并重用它

iibxawm4  于 2023-10-19  发布在  React
关注(0)|答案(4)|浏览(133)

我正在尝试将一个基本的React组件发布到我的npm注册表中,并尝试重用它。我想我没有遵循正确的方式来分发我的react组件。这是我所拥有的:
这是目录结构:

  1. MyReactPOC
  2. -> main.jsx
  3. -> .npmrc
  4. -> package.json
  5. -> webpack.config.js

main.jsx

  1. import React from 'react';
  2. class MyComponent extends React.Component {
  3. render() {
  4. return (
  5. <div>
  6. <p>Hello from MyComponent!!</p>
  7. </div>
  8. );
  9. }
  10. }
  11. export default MyComponent

package.json

  1. {
  2. "name": "@pankaj/my-component",
  3. "version": "1.0.7",
  4. "description": "POC for importing a component",
  5. "main": "./dist/bundle.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1",
  8. "prepublish": "webpack --config webpack.config.js"
  9. },
  10. "repository": {
  11. "type": "git",
  12. "url": "my git repo"
  13. },
  14. "author": "Pankaj",
  15. "license": "ISC",
  16. "dependencies": {
  17. "react": "~15.5.4",
  18. "react-dom": "~15.5.4"
  19. },
  20. "devDependencies": {
  21. "babel-cli": "~6.24.1",
  22. "babel-core": "~6.24.1",
  23. "babel-loader": "~6.4.1",
  24. "babel-preset-es2015": "~6.24.1",
  25. "babel-preset-react": "~6.24.1",
  26. "webpack": "~2.4.1"
  27. }
  28. }

webpack.config.js

  1. var path = require('path');
  2. var webpack = require('webpack');
  3. module.exports = {
  4. entry: './main.jsx',
  5. output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js' },
  6. module: {
  7. loaders: [
  8. {
  9. test: /.jsx?$/,
  10. loader: 'babel-loader',
  11. exclude: /node_modules/,
  12. query: {
  13. presets: ['es2015', 'react']
  14. }
  15. }
  16. ]
  17. },
  18. };

我使用import MyComponent from '@pankaj/my-component'在另一个项目中导入模块。
当我使用这个组件时,
我得到以下错误:
React.createElement:类型无效--应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:您可能忘记了从定义组件的文件中导出组件。
请帮助我理解分发react组件的正确方法,以便它们可以被我组织中的其他项目使用。
下面是我如何使用这个组件:

使用.js

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import MyComponent from '@pankaj/my-component';
  4. ReactDOM.render(
  5. <MyComponent/>,
  6. document.getElementById('root')
  7. );

我有一个index.html有'根' div。

xwbd5t1u

xwbd5t1u1#

每个react组件都需要返回语句。在render函数中添加一个return语句,它应该可以工作。

  1. ...
  2. render() {
  3. return (<div>...</div>)
  4. }

你不能直接从react组件渲染到Dom,而是返回它,以便react可以使用它。
在webpack中,使用output.library https://webpack.js.org/concepts/output/#output-library将输出文件指定为库。

5fjcxozz

5fjcxozz2#

我写了一个完整的中等故事,因为我有同样的问题,因为没有关于它的信息。请查看:https://medium.com/@BrodaNoel/how-to-create-a-react-component-and-publish-it-in-npm-668ad7d363ce
主要的修复是在webpack.js.js文件中添加libraryTarget: 'umd'

r8uurelv

r8uurelv3#

如果你使用babel的es6语法导出,你的组件将在MyComponent.default命名空间中。为了避免这种情况,你应该安装:

  1. npm i --save-dev babel-plugin-add-module-exports in your .babelrc?

并将其添加到babel conf中:

  1. {
  2. "presets": [ "es2015", "react"],
  3. "plugins": ["add-module-exports"]
  4. }
i7uq4tfw

i7uq4tfw4#

经过几天的搜索,我相信microbundle毫不费力地实现了这一点。下面是我的build命令:

  1. "scripts": {
  2. "build": "microbundle build --globals react=React,react-dom=ReactDOM --jsx cloneElement,createContext,isValidElement,useContext,useReducer --jsxFragment Fragment --jsxImportSource react",
  3. "dev": "microbundle watch"
  4. },

相关问题