简体   繁体   English

从 React 中的 firebase 获取映射图像数组的 url

[英]Getting url's of mapped image array from firebase in React

I have a functioning image slider in the site I am building ( https://github.com/kimcoder/react-simple-image-slider ) that takes in an array of images that are input via Object.values(images) .我在我正在构建的站点( https://github.com/kimcoder/react-simple-image-slider )中有一个正常工作的图像 slider ,它接收通过Object.values(images)输入的图像数组。 I want to swap over to a grid array ( https://github.com/benhowell/react-grid-gallery ).我想换成grid阵列( https://github.com/benhowell/react-grid-gallery )。

I am having trouble getting the image urls from the object array and displaying them.我无法从 object 数组中获取图像 url 并显示它们。

This is what my Firebase DB image structure looks like inside the post itself:这就是我的 Firebase DB 图像结构在帖子本身内部的样子: 在此处输入图像描述

Here's the code I have from the page:这是我从页面获得的代码:

import React, { useContext, useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { DatabaseContext } from '../contexts/database-context';
import Gallery from 'react-grid-gallery';
import '../scss/pages/HomeDetailPage.scss'

function HomeDetailPage() {
    const { GetSelectedListingDetail, GetListingOwner, selectedListing, listingOwner } = useContext(DatabaseContext);
    const {
        address,
        bathrooms,
        bedrooms,
        creationDate,
        description,
        images,
        postKey,
        latitude,
        longitude,
        postRef,
        postUrl,
        poster,
        price,
        rawPrice,
        squareFeet,
        view_Count,
        isHighlighted
    } = selectedListing || '';

    const { email } = listingOwner || '';
    const params = useParams();

    const [isLoading, setIsLoading] = useState(true);

    const getImages = (images) => {
        return Object.values(images)
    }

    useEffect(() => {
        const { id } = params;
        GetSelectedListingDetail(id)
    }, [params]);

    useEffect(() => {
        if (selectedListing !== null) {
            setIsLoading(false);
        }
    }, [selectedListing]);

    useEffect(() => {
        if (poster) {
            GetListingOwner(poster)
        }
    }, [poster])

    if (isLoading && selectedListing === null) {
        return <progress className="progress is-small is-info" max="100">60%</progress>

    }

    const IMAGES =
    [{
            src: "https://c2.staticflickr.com/9/8817/28973449265_07e3aa5d2e_b.jpg",
            thumbnail: "https://c2.staticflickr.com/9/8817/28973449265_07e3aa5d2e_n.jpg",
            thumbnailWidth: 320,
            thumbnailHeight: 174,
            isSelected: true,
            caption: "After Rain (Jeshu John - designerspics.com)"
    },
    {
            src: "https://c2.staticflickr.com/9/8356/28897120681_3b2c0f43e0_b.jpg",
            thumbnail: "https://c2.staticflickr.com/9/8356/28897120681_3b2c0f43e0_n.jpg",
            thumbnailWidth: 320,
            thumbnailHeight: 212,
            tags: [{value: "Ocean", title: "Ocean"}, {value: "People", title: "People"}],
            caption: "Boats (Jeshu John - designerspics.com)"
    },
    
    {
            src: `${Object.values(images).toString()}` //doesn't load image
    }]


    return (
        <div className='detailBg'>
            <div className='home-detail-page'>
                <div className="home-detail-page-col-1">
                    {/*images go here */}
                    <Gallery images={Object.values([images])}/>
                </div>
                <div className="home-detail-page-col-2">
                    <div className="detailDiv">
                        <div className="propInfo">
                            <p className="title is-2 mb-5">{price}</p>
                            <p className="subtitle is-6 mb-2">{address}</p>
                            <p className="subtitle is-6">{bathrooms + ' ' + bedrooms + ' ' + squareFeet}</p>
                        </div>
                        <div className="contactBtn">
                            <a className="button is-info" href={`mailto:${email}`}>Contact Owner</a>
                        </div>
                        <div className="propDesc">
                            <p className="title is-5">Property Overview:</p>
                            <p className="title is-6 pb-4">
                                {description}
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default HomeDetailPage;

You need at least src and thumbnail on each image.每个图像至少需要srcthumbnail Here is the correct code:这是正确的代码:

<Gallery
  images={Object.values(images).map((url) => ({
    src: url,
    thumbnail: url
  }))}
/>

Working example 工作示例

The library has not been updated for years.图书馆多年没有更新。 It only seems to work for React 16. I definitely don't recommend using it as is.它似乎只适用于 React 16。我绝对不建议按原样使用它。 Only if you fork and update everything.仅当您分叉并更新所有内容时。

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

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