简体   繁体   English

我如何显示图像阵列?

[英]How could i display an array of images?

I have a react page where the data are fetched using an api. 我有一个反应页面,其中使用api提取数据。 All the data are fetched successfully but the array of images could not get fetched. 已成功获取所有数据,但无法获取图像数组。 The code that i have written for this is 我为此编写的代码是

import React, {Component} from 'react';

export default class RoomDetail extends Component{
    constructor(props){
        super(props);
        this.state = { rooms:[] }
    }
    componentDidMount(){
        console.log('componentDidMount');
        console.log(this.props.data.slug);
        this.loadRoomFromServer();
    }
    loadRoomFromServer(){
        $.ajax({
            url:'/api/v1/rental/'+this.props.data.slug,
            dataType:'json',
            success: (data) => {
                console.log('data',data);
                this.setState({rooms: data});
              },
              error: (xhr, status, err) => {
                console.error(url, status, err.toString());
              }
            });
    }
    render() {
        if(this.state.rooms.gallery){
            console.log(this.state.rooms.gallery);
              let imageFile = this.state.rooms.gallery.map((image) => {
            return(
                    <img src={image.image} className="img-responsive" />
                );
            });

        }
        if(this.state.rooms){
            console.log('rooms',this.state.rooms);
            return (
            <div className="container-fluid">
                <div className="row">
                    <div className="col-sm-12">
                        <img src="" />
                    </div>
                </div>
                <div className="col-md-6">
                    <ul className="list-group">
                        <li>Listing Name</li>
                        <li>Summary on Listing</li>
                        <li>Property Type</li>
                        <li>No of Rooms</li>
                        <li>Gallery</li>
                    </ul>
                </div>
                <div className="col-md-6">
                    <ul className="list-group">
                        <li>{this.state.rooms.listingName}</li>
                        <li>{this.state.rooms.summary}</li>
                        <li>{this.state.rooms.property}</li>
                        <li>{this.state.rooms.room}</li>
                        <li>{this.state.rooms.amenities}</li>
                    </ul>
                </div>
            </div>
            );
        }

        else{
            return <li>Nothing to display</li>
        }

    }
}

How can i display all the images ? 如何显示所有图像? If i used the variable imageFile after this.state.rooms.amenities,i get an error saying imageFile is not defined. 如果我在this.state.rooms.amenities之后使用了变量imageFile,则会收到一条错误消息,提示未定义imageFile。 imageFile is not loaded so it might be saying not defined. imageFile未加载,因此可能是未定义。

This is not React specific, it's just plain javascript. 这不是特定于React的,只是纯JavaScript。 The let keyword defines a block-scoped variable, ie it only exists in the if statement where it resides. let关键字定义了一个块作用域变量,即它仅存在于它所在的if语句中。 If you want it to be available elsewhere, you should define it outside of the if: 如果要在其他地方使用它,则应在if之外定义它:

let imageFile; // Define the variable outside of the if statement
if(this.state.rooms.gallery){
        console.log(this.state.rooms.gallery);

        // Dump the `let` keyword inside of the if statement
        imageFile = this.state.rooms.gallery.map((image) => {
        // ...
    });
}
if (this.state.rooms) {
    // Now `imageFile` can be accessed here too
}

Since the first if statement checks for a property of the second one, I would prefer rationalizing it like: 由于第一个if语句检查第二个属性的属性,因此我希望将其合理化,例如:

if (this.state.rooms) {
    let imageFile;

    if (this.state.rooms.gallery) {
        // Assign `imageFile here`
    }
}

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

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