我学习了Reacjs和javascript,现在遇到了一个问题,就是永远不会调用{WithFetching(XlsxVie...
function
下面的内容。
WithFetching
应该以{{1}}和XlsxViewer
作为参数来调用,但是我错过了一些不知道的东西,因为它不起作用..:
newProps
这是import WithFetching from '../file-viewer/fetch-wrapper';
const newProps = { ...this.props, responseType: 'arraybuffer', fileType: 'xlsx', filePath: { theFile } };
return (
<div className="pg-viewer-wrapper">
<div className="pg-viewer" id="pg-viewer">
{WithFetching(XlsxViewer, newProps)};
</div>
</div>
);
}
WithFetching
这是import React, { Component } from 'react';
import Error from './error';
import Loading from './loading';
function WithFetching(WrappedComponent, props) {
return class FetchComponent extends Component {
constructor(props) {
// eslint-disable-line no-shadow
super(props);
this.state = {};
this.xhr = this.createRequest(props.filePath);
}
componentDidMount() {
try {
this.fetch();
} catch (e) {
if (this.props.onError) {
this.props.onError(e);
}
this.setState({ error: 'fetch error' });
}
}
componentWillUnmount() {
this.abort();
}
createRequest(path) {
let xhr = new XMLHttpRequest();
if ('withCredentials' in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open('GET', path, true);
// } else if (typeof XDomainRequest !== 'undefined') {
// // XDomainRequest for IE.
// xhr = new XDomainRequest();
// xhr.open('GET', path);
} else {
// CORS not supported.
xhr = null;
return null;
}
if (props.responseType) {
xhr.responseType = props.responseType;
}
xhr.onload = () => {
if (xhr.status >= 400) {
this.setState({ error: `fetch error with status ${xhr.status}` });
return;
}
const resp = props.responseType ? xhr.response : xhr.responseText;
this.setState({ data: resp });
};
return xhr;
}
fetch() {
this.xhr.send();
}
abort() {
if (this.xhr) {
this.xhr.abort();
}
}
render() {
if (!this.xhr) {
return <h1>CORS not supported..</h1>;
}
if (this.state.error) {
return <Error {...this.props} error={this.state.error} />;
}
if (this.state.data) {
return <WrappedComponent data={this.state.data} {...this.props} />;
}
return <Loading />;
}
};
}
export default WithFetching;
XlxsViewer
为什么不叫import React, { Component } from 'react';
import XLSX from 'xlsx';
import CsvViewer from './csv-viewer';
class XlxsViewer extends Component {
constructor(props) {
super(props);
this.state = this.parse();
}
parse() {
const dataArr = new Uint8Array(this.props.data);
const arr = [];
for (let i = 0; i !== dataArr.length; i += 1) {
arr.push(String.fromCharCode(dataArr[i]));
}
const workbook = XLSX.read(arr.join(''), { type: 'binary' });
const names = Object.keys(workbook.Sheets);
const sheets = names.map(name => XLSX.utils.sheet_to_csv(workbook.Sheets[name]));
return { sheets, names, curSheetIndex: 0 };
}
renderSheetNames(names) {
const sheets = names.map((name, index) => (
<input
key={name}
type="button"
value={name}
onClick={() => {
this.setState({ curSheetIndex: index });
}}
/>
));
return <div className="sheet-names">{sheets}</div>;
}
renderSheetData(sheet) {
const csvProps = Object.assign({}, this.props, { data: sheet });
return <CsvViewer {...csvProps} />;
}
render() {
const { sheets, names, curSheetIndex } = this.state;
return (
<div className="spreadsheet-viewer">
{this.renderSheetNames(names)}
{this.renderSheetData(sheets[curSheetIndex || 0])}
</div>
);
}
}
export default XlxsViewer;
?
答案 0 :(得分:1)
代替打电话
WithFetching(XlsxViewer, newProps)
将其更改为:
<XlsxViewer newProps={newProps} />
并在XlxsViewer.js中进行更改
export default XlxsViewer;
具有:
export default WithFetching(XlxsViewer);
在WithFetching.js中,更改
function WithFetching(WrappedComponent, props) {
具有:
function WithFetching(WrappedComponent) {
和newProps在FetchComponent中作为this.props访问
答案 1 :(得分:0)
WithFecthing
返回一个类,而不是一个类的实例。
所以不要打电话
WithFetching(XlsxViewer, newProps)
您必须致电
new WithFetching(XlsxViewer, newProps)
请记住,以这种方式使用它,每次调用父组件时都会重新创建该组件。
最好创建一个名为FetchingXlsxViewer
的组件,该组件仅将WithFetching(XlsxViewer, newProps)
导出为默认值。