gpt4 book ai didi

javascript - 无法渲染函数返回的 react 元素

转载 作者:行者123 更新时间:2023-11-28 14:44:58 25 4
gpt4 key购买 nike

下面是我的 FileImport 组件,

import React, {Component} from 'react';
import FileInput from 'react-simple-file-input';
import XLSX from 'xlsx';

var allowedFileTypes = ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];

function fileIsIncorrectFiletype(file){
if (allowedFileTypes.indexOf(file.type) === -1) {
return true;
} else {
return false;
}
}

class FileImport extends Component {
constructor(props, context) {
super(props, context);

this.cancelButtonClicked = this.cancelButtonClicked.bind(this);
this.resetCancelButtonClicked = this.resetCancelButtonClicked.bind(this);
this.showProgressBar = this.showProgressBar.bind(this);
this.updateProgressBar = this.updateProgressBar.bind(this);
this.handleFileSelected = this.handleFileSelected.bind(this);
this.state = {
cancelButtonClicked: false
};
}

render(){
return(
<div>
To upload a file:

<label >
<FileInput
readAs='binary'
style={ { display: 'none' } }

onLoadStart={this.showProgressBar}
onLoad={this.handleFileSelected}
onProgress={this.updateProgressBar}
cancelIf={fileIsIncorrectFiletype}
abortIf={this.cancelButtonClicked}
onCancel={this.showInvalidFileTypeMessage}
onAbort={this.resetCancelButtonClicked}
/>

<span >
Click Here
</span>

</label>
<span>
{this.handleFileSelected()}
</span>
</div>
);
}

cancelButtonClicked(){
return this.state.cancelButtonClicked;
}

resetCancelButtonClicked(){
this.setState({ cancelButtonClicked: false });
}

showInvalidFileTypeMessage(file){
window.alert("Tried to upload invalid filetype " + file.type);
}

showProgressBar(){
this.setState({ progressBarVisible: true});
}

updateProgressBar(event){
this.setState({
progressPercent: (event.loaded / event.total) * 100
});
}

handleFileSelected(event, file){
this.setState({file: file, fileContents: event.target.result});
var reader = new FileReader();
var fileContents = event.target.result;
var workbook = XLSX.read(fileContents, {
type: 'binary'
});
workbook.SheetNames.forEach(function(sheetName) {
var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
let studentList = [];
for(let i =0; i<XL_row_object.length; i++){
studentList.push(
<table>
<tbody>
<tr>
<td type="text" value={XL_row_object[i].Roll_Number} />
<td type="text" value={XL_row_object[i].Name} />
<td type="text" value={XL_row_object[i].Attendance} />
</tr>
</tbody>
</table>
)
}
return studentList || null;
})
reader.onerror = function(ex) {
console.log(ex);
};

reader.readAsBinaryString(file);
};

}

export default FileImport;

这是我的另一个组件,其中包含 FileImport,

import React, {Component} from 'react';
import Checkbox from 'react-checkbox';
import Login from './Login';
import FileImport from './FileImport'
import '../Styles/Attendance.css'
import JsonTable from 'react-json-table';

class Attendance extends Component{
render(){
var checked = null;
return(
<div>
<Login />
<h3 className="text-center"> Attendance sheet for the student</h3>
<span className="heading">
<label id="heading-font"> Roll Number</label>
<label id="heading-font"> Name of the Student</label>
<label id="heading-font"> Attendance </label>
<input type="date" />
</span>
<FileImport />

</div>
);
}
}
export default Attendance;

当我运行它时,它会抛出以下错误,

Errors in console - 1

Errors is console - 3

如果我使用 {this.handleFileSelected()} 调用该函数,则会发生这种情况,但是如果我使用 {this.handleFileSelected} 调用该函数,它不会抛出错误,但不会显示数据,尽管返回对象具有所有需要显示的元素。

最佳答案

您应该简单地调用您的函数:

{this.handleFileSelected()}

关于javascript - 无法渲染函数返回的 react 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com