Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
React boostrap carousel not working when using array.map function in react

I'm trying to create a simple carousel using react Bootstap but it seems not to work. The code below doesn't work

function CarouselItem({ item }) {

 return (
   <Carousel.Item>
  <img
    className="d-block w-100"
    src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQom33pPRha3xPSmmGVI5zaqSiraDxb_KuMKvmIOnfQb3rbHZZKJ8wxRGttgjfDpQ3cKrQ&usqp=CAU"
    alt="First slide"
  />
  <Carousel.Caption>
    <h3>{`${item} slide label`}</h3>
    <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
  </Carousel.Caption>
</Carousel.Item>
 );
}

function App() {
    return (
     <div className="App">
  <Carousel>
    {[1, 2, 3, 4, 5, 6].map((item) => {
      return <CarouselItem key={item} item={item} />;
    })}
  </Carousel> 
</div>
 );
}

but this code works very fine. Is there a way i can make the first code work? Because I have to fetch data in the CarouselItem component for each item before i render it that is why I want to use the first approach.

function App() {
  return (
    <div className="App">
  {/**but this way works**/}
  <Carousel>
    {[1, 2, 3, 4, 5, 6].map((item) => {
      return (
        <Carousel.Item>
          <img
            className="d-block w-100"
            src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQom33pPRha3xPSmmGVI5zaqSiraDxb_KuMKvmIOnfQb3rbHZZKJ8wxRGttgjfDpQ3cKrQ&usqp=CAU"
            alt="First slide"
          />
          <Carousel.Caption>
            <h3>{`${item} slide label`}</h3>
            <p>
              Nulla vitae elit libero, a pharetra augue mollis interdum.
            </p>
          </Carousel.Caption>
        </Carousel.Item>
      );
    })}
  </Carousel> 
</div>
 );
}

you can find the sandbox code here sandbox code

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Pretty sure this would work

<Carousel>
 {[1, 2, 3, 4, 5, 6].map((item) => {
   return <Carousel.Item key={item}>{item}</Carousel.Item>;
 })}
</Carousel>

In React-Bootstrap docs I cannot find property item for <Carousel.Item />, you must pass children instead.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!