【发布时间】:2020-12-08 23:02:34
【问题描述】:
Hello Guys 是 reactjs 的新手,我正在尝试获取响应数据对象,以便能够在 webapp 上显示以供用户查看,而不是检查页面并转到网络/控制台查看文件上传错误响应name= 'title' 所以我决定使用下面的函数来给我数据响应,我不断收到这个错误 'renderItems is not defined' no-undef 下面是示例代码:
import React from "react";
import PropTypes from "prop-types";
import axios from 'axios'
import { Card,} from "shards-react";
class Uploaddatastore extends React.Component {
constructor(props) {
super(props);
this.state = {
UploadNotification: []
}
}
componentDidMount(){
let notificationComponent = this;
axios.get("http://127.0.0:8000/uploads/file_upload/")
.then(function (response) {
console.log(response);
console.log(response.data);
notificationComponent.setState({
UploadNotification: response.data.items
});
})
.catch(function (error) {
console.log(error);
});
}
render() {
if (this.state.UploadNotification.length) {
let renderItems = this.state.UploadNotification.map(function(item, i) {
return <li key={i}>{item.title}</li>
});
}
return (
<div>
<span className="text-danger"> {renderItems} </span>
</div>
);
}
};
export default Uploaddatastore;
【问题讨论】:
-
let是块作用域。将该语句移到if之外。 (事实上,您似乎根本不需要if。) -
您好,如果您能帮助使该功能能够在页面上为用户呈现控制台响应,您将不胜感激?
标签: javascript django reactjs django-rest-framework