简体   繁体   English

图像阵列 ReactJs 响应式轮播

[英]Array of Images ReactJs responsive carousel

I am just getting started with web pages.我刚刚开始使用 web 页面。 I am trying to make a function for the react responsive carousel so I can pass an array of images and it renders depending on the number of images, I've been trying to run something like this:我正在尝试为反应响应轮播制作 function 以便我可以传递一组图像并根据图像的数量进行渲染,我一直在尝试运行这样的东西:

Callingfun.js调用乐趣.js

import React from 'react';

import Carouselfun from '../components/Carouselfun'

import webs from '../assets/images/proj/website.JPG'
import webs1 from '../assets/images/proj/website1.JPG'
import webs2 from '../assets/images/proj/website2.JPG'


const imagess= [webs,webs1,webs2];

function WebsitePage(props) {
    return(
        <div className="g-background-pages">
            
            <Carouselfun   
                        imag={imagess}
                        />
        </div>
    );

}

export default WebsitePage;

Carouselfun.js Carouselfun.js

import React from 'react';
import {Jumbotron, Container, Row, Col, Image} from 'react-bootstrap';

import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loader
import { Carousel } from 'react-responsive-carousel';


function carouselfun(props){
    
    return(
        <Jumbotron>
            <Container>
                 <Row>
                    <div>
                        <div>        
                            <Carousel>
                             
                            {props.imag}.forEach(element => {
                                <div>
                                <Image src= element/>
                                </div>
                            });
                                
                            </Carousel>         
                        </div>
                    </div>
                </Row>
            </Container>
        </Jumbotron>
    )
}

export default carouselfun;

Is there something wrong in the way I am passing my arrays of Images?我传递图像的 arrays 的方式有什么问题吗? or does it have to be with the render function?还是必须与渲染 function 一起使用?

Thank you谢谢

It seems that you are using array.forEach, where a array.map operation will do instead.看来您正在使用 array.forEach,其中将使用 array.map 操作。 Map will transform your array and return an array of <div><Image src={element}/> </div> (ReactNodes). Map 将转换您的数组并返回<div><Image src={element}/> </div> (ReactNodes) 的数组。 Foreach wont return anything. Foreach 不会返回任何东西。 As Talmacel notes in the comment you were missing some brackets too.正如 Talmacel 在评论中指出的那样,您也缺少一些括号。

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

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