我想構建一個組件,可以用作npm包,並構建了一個示例應用程序來測試和演示用法。這導致在React組件不被識別爲字符串或類函數
我有兩個文件:在包源中:PDF
和index
。 PDF定義在最後的組件和出口與
export default PDF;
index.js
只是export {default} from './PDF';
編輯 - PDF文件
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import Viewer from './Viewer';
import pdfjsLib from './../node_modules/pdfjs-dist/webpack'
class PDF extends Component {
constructor(props) {
super(props);
this.state = {
document: null
}
}
componentDidMount() {
const loadingTask = pdfjsLib.getDocument(this.props.file);
loadingTask.promise.then(document => {
console.log('Loaded Document');
console.log(document);
this.setState({
document: document
})
}).catch(err => {
console.log(err);
});
}
render() {
return(
<div>
<Viewer doc={this.state.document}/>
</div>
)
}
}
export {PDF};
的WebPack建立配置
module.exports = {
entry: {
'index': path.join(__dirname, 'src', 'index.js')
},
output: {
path: path.join(__dirname, '/lib'),
filename: '[name].js',
sourceMapFilename: '[name].map'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: ['url-loader']
},
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: "babel-loader",
options: {
presets: ["es2015", "react", "stage-2"]
}
}
},
]
}
};
它創建了一個/lib
文件夾爲BU ild artifacts。
我那麼這個符號鏈接build文件夾到project/example/node_modules/react-pdf
併爲下面的示例演示使用它:
import React, {Component} from 'react';
import PDF from 'react-pdf';
class Viewer extends Component {
constructor(props) {
super(props);
this.state = {
file: '//s3-us-west-2.amazonaws.com/bucket-test/test-file-43092.pdf'
}
}
render() {
return(
<div>
<PDF document={this.state.file}/>
</div>
)
}
}
export default Viewer;
但是,這導致在開始時的屏幕截圖。
其他的事情我已經試過:
- 項目/ src目錄/ index.js
export {PDF} from './PDF'
; - 項目/ SRC/index.js
export {PDF as default} from './PDF';
- 項目/ SRC/PDF.js
export {PDF};
- 項目/示例/ SRC/Viewer.js
import {PDF} from 'react-pdf'
;
但沒有任何工作。我懷疑這與webpack構建配置有關,但沒有出現在我身上。
可以請你上傳到github上,這樣我們就可以測試它。 –
您是否嘗試過在index.js中從'./PDF'導入PDF;出口默認PDF;' –
@PiyushDhamecha,不能做: - /這是一個工作項目,沒有公共github回購可用(和管理不會讓我分叉我的個人帳戶) – Jason