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

iibxawm4  于 12个月前  发布在  React
关注(0)|答案(4)|浏览(82)

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

MyReactPOC
    -> main.jsx
    -> .npmrc
    -> package.json
    -> webpack.config.js

main.jsx

import React from 'react';

class MyComponent extends React.Component {
    render() {
        return (
            <div>
                <p>Hello from MyComponent!!</p>
            </div>
        );
    }
}

export default MyComponent

package.json

{
  "name": "@pankaj/my-component",
  "version": "1.0.7",
  "description": "POC for importing a component",
  "main": "./dist/bundle.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepublish": "webpack --config webpack.config.js"
  },
  "repository": {
    "type": "git",
    "url": "my git repo"
  },
  "author": "Pankaj",
  "license": "ISC",
  "dependencies": {
    "react": "~15.5.4",
    "react-dom": "~15.5.4"
  },
  "devDependencies": {
    "babel-cli": "~6.24.1",
    "babel-core": "~6.24.1",
    "babel-loader": "~6.4.1",
    "babel-preset-es2015": "~6.24.1",
    "babel-preset-react": "~6.24.1",
    "webpack": "~2.4.1"
  }
}

webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: './main.jsx',
    output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js' },
    module: {
        loaders: [
            {
                test: /.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    },
};

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

使用.js

import React from 'react';
import ReactDOM from 'react-dom';
import MyComponent from '@pankaj/my-component';

ReactDOM.render(
  <MyComponent/>,
  document.getElementById('root')
);

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

xwbd5t1u

xwbd5t1u1#

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

...
render() {
     return (<div>...</div>)
}

你不能直接从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命名空间中。为了避免这种情况,你应该安装:

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

并将其添加到babel conf中:

{
  "presets": [ "es2015", "react"],
  "plugins": ["add-module-exports"]
}
i7uq4tfw

i7uq4tfw4#

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

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

相关问题