【问题标题】:RenderItems is not defined no-undef inside returnRenderItems 未定义 no-undef 内返回
【发布时间】: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


【解决方案1】:

您对 renderItems 变量的范围错误。它仅限于if 块。在 if 之前声明它:

render() {
    let renderItems;
    if (this.state.UploadNotification.length) {
      renderItems = this.state.UploadNotification.map(function(item, i) {
          return <li key={i}>{item.title}</li>
      });
    }
    return (
     <div>
       <span className="text-danger"> {renderItems} </span>
     </div>
    );
  }
};

【讨论】:

  • Christian Fritz 成功了,谢谢,虽然我希望能帮助我从网络/控制台获取对象并将其呈现在 webbApp 页面上供用户使用,这可能吗?
猜你喜欢
  • 2021-10-16
  • 2018-06-09
  • 2018-10-02
  • 2021-05-26
  • 2021-06-17
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多