Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to append "active" class on first div using loop in Reactjs

I am working in Reactjs and using nextjs framework,right now i fetching data (videos) in slider and i want by default "active class" should be added to first value of array, In other words before click on slide i want by default value should "active" and should remove after click on "slide arrow" (< or >) button,How can i do this ? Here is my code

{this.state.trending.map((post, index) => {
return (
    <>
        <div class="carousel-item active">
        <YouTube videoId={post.VideoId} opts={opts} />
        </div>
    </>
     )
 })}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This should work:

{this.state.trending.map((post, index) => {
   return (
    <>
      <div class={`carousel-item ${index===0 ? "active" : ""`}>
        <YouTube videoId={post.VideoId} opts={opts} />
      </div>
    </>
   )
})}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading