简体   繁体   English

使用 map function ReactJS 显示为未定义的链接

[英]Links showing as undefined using map function ReactJS

Hi I'm new in react so sorry in advance if I missed something obvious.嗨,我是新手,如果我错过了明显的事情,请提前做出反应。 I'm having problems with returning links from my json while using the map function.我在使用 map function 时从 json 返回链接时遇到问题。 I have been trying to use the map function to return several links that are in an array in json file that I have but I seem to be doing something wrong.我一直在尝试使用 map function 来返回 json 文件中的几个链接,但我似乎做错了什么。 I would like to go through the array "Images" and get each link and print it in the console (For now. I will use it later to show the image).我想通过数组“Images”来 go 并获取每个链接并在控制台中打印它(现在。我稍后将使用它来显示图像)。 Here is the result of using the code below: [.[Using file.images.src][1]][1] If I do file:images it returns this.这是使用以下代码的结果: [.[Using file.images.src][1]][1] 如果我执行 file:images 它会返回这个。 [![Using file.images][2]][2] (I have no idea why it prints twice) Here is my code [![Using file.images][2]][2] (我不知道为什么会打印两次)这是我的代码

import React, { Component } from 'react';


// Project Detail Class
class ProjectDetail extends Component {

constructor() {
    super();
    this.state = {
        data: [
            {
                "id": 19,
                "title": "Example 1",
                "address": "Example -address",
                "address2": "Example -address",
                "city": "Example City",
                "zipcode": "00000",
                "client": "Example 1 llc",
                "commercial": true,
                "residential": false,
                "completed": "Completed",
                "featured": true,
                "images": [
                    {
                        "src": "http://127.0.0.1:8000/media/images/waves.jpg"
                    },
                    {
                        "src": "http://127.0.0.1:8000/media/images/volcano.jpg"
                    },
                    {
                        "src": "http://127.0.0.1:8000/media/images/city.jpg"
                    }
                ]
            }
        ]
    }
}


// Render the page 
render() {
    return (
        <div>
            {this.state.data.map((file) => {
                return console.log(file.images.src)
                
            }
            )}
        </div>

    );
}
} export default ProjectDetail;

The result I'm looking for is:我正在寻找的结果是:

  1. http://127.0.0.1:8000/media/images/waves.jpg http://127.0.0.1:8000/media/images/waves.jpg
  2. http://127.0.0.1:8000/media/images/volcano.jpg http://127.0.0.1:8000/media/images/volcano.jpg
  3. http://127.0.0.1:8000/media/images/city.jpg http://127.0.0.1:8000/media/images/city.jpg

The json file format is done by django-rest-framework (maybe thats causing it) Thank you for your time:) json 文件格式是由 django-rest-framework 完成的(也许就是这样)谢谢你的时间:)

You can't return a console.log function call like this.您不能像这样return console.log function 调用。 You have to return either a JSX element (like a div ) or null .您必须返回 JSX 元素(如div )或null

I also noticed you're trying to log images.src , which doesn't exist.我还注意到您正在尝试记录不存在的images.src images is an array of objects, each containing a src property, so you need to map over that array separately and log each value if you want separate logs for each. images是一个对象数组,每个对象都包含一个src属性,因此您需要分别对该数组进行 map 并记录每个值,如果您希望为每个值单独记录。 See my example below.请参阅下面的示例。

If you don't want to render anything and just see the logs you can log first and then return null like this:如果您不想渲染任何内容而只查看日志,您可以先登录,然后像这样返回 null:

  render() {
    return (
      <div>
        {this.state.data.map((file) => {
          file.images.forEach((image) => console.log(image.src));

          return null;
        })}
      </div>
    );
  }

Bear in mind this will log to your console on every render.请记住,这将在每次渲染时记录到您的控制台。

you can do this你可以这样做

import React, { Component } from 'react';


// Project Detail Class
class ProjectDetail extends Component {

constructor() {
    super();
    this.state = {
        data: [
            {
                "id": 19,
                "title": "Example 1",
                "address": "Example -address",
                "address2": "Example -address",
                "city": "Example City",
                "zipcode": "00000",
                "client": "Example 1 llc",
                "commercial": true,
                "residential": false,
                "completed": "Completed",
                "featured": true,
                "images": [
                    {
                        "src": "http://127.0.0.1:8000/media/images/waves.jpg"
                    },
                    {
                        "src": "http://127.0.0.1:8000/media/images/volcano.jpg"
                    },
                    {
                        "src": "http://127.0.0.1:8000/media/images/city.jpg"
                    }
                ]
            }
        ]
    }
}


// Render the page 
render() {
    return (
        <div>
            {this.state.data.map((file) => {
                  {file.images.map((images) => {
                  console.log(images.src)
                   return null;
                   })}
               }
            )}
        </div>

    );
}
} export default ProjectDetail;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM