简体   繁体   English

如何在ReactJS中使用Object.key.map()?

[英]How to use Object.key.map() in ReactJS?

I am studying ReactJS, and making a website with it and Firebase. 我正在研究ReactJS,并使用它和Firebase创建一个网站。

I wanted to display all images in my Firebase Stroage, but I don't know how to do that, and my code does't work. 我想在Firebase Stroage中显示所有图像,但是我不知道该怎么做,并且我的代码不起作用。

I got the all url of images in Firebase Storage, and put these into array. 我在Firebase Storage中获得了所有图像的url,并将它们放入数组中。 when I console.log(imageObejct), it shows all links correctly. 当我console.log(imageObejct)时,它正确显示了所有链接。

But my render (object.key.map) doesn't work. 但是我的渲染(object.key.map)不起作用。

I tried different ways, but didn't work. 我尝试了不同的方法,但是没有用。

Help! 救命!

I have tried 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description 我已经尝试过1. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description

  1. https://flaviocopes.com/react-how-to-loop/ https://flaviocopes.com/react-how-to-loop/
class ImageUpload extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            image: [],
            user: this.props.user,
            url: '',
            progress: 0,
            dialog: false,
            fileName: '',
            file: '',
            imagePreviewUrl: '',
            title: '',
            content: '',
            imagePath: [],
            imageObject: ''
        };

////////////////////////////////////////////////////////
 displayImage = (images) => {

        // Get the download URL

        images.getDownloadURL().then(data => {
            // Insert url into an <img> tag to "download"
            this.setState({ imageObject : [(this.state.imageObject == null) ? null : data] })

           // console.log(this.state.imageObject)
        }).catch(error => {
//////////////////////////////////////////
    render() {
        const { classes } = this.props;
        return (
             <div>
                {Object.keys(this.state.imageObject).map(id => {
                    const img = this.state.imageObject[id];
                    return (
                        <Card key={id}>
                            <CardContent>
                                <Typography>
                                    Title: 
             ///here!!//              <img src={img}/>
                                </Typography>




I expected all images in my Firebase Storage 我希望Firebase存储中的所有图像

but it just shows me only one(first one) image out of 12. 但这只显示了12张中的一张(第一个)图像。

Firstly set your imageObject to empty array as default, if it is an array (probably it'd be named imageArray then): 首先,将您的imageObject设置为默认的空数组(如果它是一个数组)(然后将其命名为imageArray ):

 this.state = {
   imageObject: []
 };

Then in your setState , add the data to your array 然后在setState ,将数据添加到数组中

 images.getDownloadURL().then(data => {
   this.setState(state => ({
     imageObject: [...state.imageObject, data]
   }))
 })

After that when you render it, you don't need to use Object.keys , but just map the array: 之后,在渲染它时,不需要使用Object.keys ,只需映射数组即可:

 return (
    <div>
    {this.state.imageObject.map(img => {
     return (
//...

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

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