S
S
Sergey2021-08-17 19:54:22
React
Sergey, 2021-08-17 19:54:22

How to print an array of children?

Hello!

There is a react-swiper library.
I need to make for the ui-kit how many wrapper components above this slider.
Slider example:

<Swiper>
        <SwiperSlide>Слайд 1</SwiperSlide>
        <SwiperSlide>Слайд 2</SwiperSlide>
        <SwiperSlide>Слайд 3</SwiperSlide>
 </Swiper>


In order to make a slide, you need to additionally push the content into the SwiperSlide component, this is what prevents wrapping it with an additional component.

wrapper example
const Carousel = ({ children }) => {
  return (
    <>
      <Swiper>
         <SwiperSlide>{ children }</SwiperSlide>
      </Swiper>
    </>
  );
};

// Вызов

<Carousel>
      <div>1</div>
       <div>2</div>
       <div>3</div>
</Carousel>


I get 1 slide, which will consist of 3 divs. Is it possible to print each child separately via SwiperSlide inside the component?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-08-17
@justedoit

<Swiper>
  {children.map(n => <SwiperSlide>{n}</SwiperSlide>)}
</Swiper>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question