Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungFliproom: a room changeover is a content problem(21.09.2026 um 04:10 Uhr)
Sichere ProgrammierungNova Adiutrix: My Second Agent Built My First Project's To-Do List(21.09.2026 um 04:11 Uhr)
Sichere ProgrammierungFliproom: a room changeover is a content problem(21.09.2026 um 04:10 Uhr)
Sichere ProgrammierungNova Adiutrix: My Second Agent Built My First Project's To-Do List(21.09.2026 um 04:11 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

How to Create a Simple Carousel/Slider in React

Reagiere als Erste:r — dein Feedback zählt!

A carousel (or slider) is a great way to display a series of images or content, one at a time. You can use buttons to navigate through them. Let's take a look at how you can build a simple carousel using React.

import { useEffect, useState } from 'react';
import { shortList, list, longList } from '../data';
import { FaQuoteRight } from 'react-icons/fa';
import { FiChevronLeft, FiChevronRight } from 'react-icons/fi';

const Carousel = () => {
  const [people, setPeople] = useState(longList);
  const [currentPersonIndex, setCurrentPersonIndex] = useState(0);

  const prevSlide = () => {
    // const prevIndex =
    //   currentPersonIndex === 0 ? people.length - 1 : currentPersonIndex - 1;
    // setCurrentPersonIndex(prevIndex);

    setCurrentPersonIndex((oldIndex) => {
      const newIndex = (oldIndex - 1 + people.length) % people.length;

      return newIndex;
    });
  };

  const nextSlide = () => {
    // const nextIndex =
    //   currentPersonIndex === people.length - 1 ? 0 : currentPersonIndex + 1;
    // setCurrentPersonIndex(nextIndex);

    setCurrentPersonIndex((oldIndex) => {
      const newIndex = (oldIndex + 1) % people.length;

      return newIndex;
    });
  };

  useEffect(() => {
    const sliderId = setInterval(() => {
      nextSlide();
    }, 2000);

    return () => {
      clearInterval(sliderId);
    };
  }, [currentPersonIndex]);

  return (
    <section className='slider-container'>
      {people.map((person, personIndex) => {
        const { id, name, image, title, quote } = person;
        return (
          <article
            key={id}
            className='slide'
            style={{
              transform: `translateX(${
                100 * (personIndex - currentPersonIndex)
              }%)`,
              opacity: personIndex === currentPersonIndex ? 1 : 0,
              visibility:
                personIndex === currentPersonIndex ? 'visible' : 'hidden',
            }}
          >
            <img src={image} alt={name} className='person-img' />
            <h5 className='name'>{name}</h5>
            <p className='title'>{title}</p>
            <p className='text'>{quote}</p>
            <FaQuoteRight className='icon' />
          </article>
        );
      })}

      <button type='button' className='prev' onClick={prevSlide}>
        <FiChevronLeft />
      </button>

      <button type='button' className='next' onClick={nextSlide}>
        <FiChevronRight />
      </button>
    </section>
  );
};
export default Carousel;

Explanation of the Functions: nextSlide and prevSlide

1. nextSlide Function:

The nextSlide function moves the carousel to the next person in the list.

const nextSlide = () => {
  setCurrentPersonIndex((oldIndex) => {
    const newIndex = (oldIndex + 1) % people.length;
    return newIndex;
  });
};

How it works:

  • The function first looks at the current person’s index, which is stored in currentPersonIndex.
  • It then adds 1 to this index (oldIndex + 1) to go to the next person.
  • The % people.length is used to ensure that when the index reaches the end of the list (e.g., at the last person), it wraps around to the first person again.

    • For example, if you are at the last person (index 4) and press "next", it will wrap back to person 0 (the first person).

Example:

  • If currentPersonIndex = 3 (person 4), then oldIndex + 1 = 4. But, because the list has 5 people (indexes 0 to 4), using % 5 gives us 4 % 5 = 0, which means it will go back to the first person.

2. prevSlide Function:

The prevSlide function moves the carousel to the previous person in the list.

const prevSlide = () => {
  setCurrentPersonIndex((oldIndex) => {
    const newIndex = (oldIndex - 1 + people.length) % people.length;
    return newIndex;
  });
};

How it works:

  • The function first looks at the current person’s index, stored in currentPersonIndex.
  • It then subtracts 1 from this index (oldIndex - 1) to go to the previous person.
  • The addition of people.length ensures that if the index goes below 0 (e.g., when we are at the first person and want to go back), it wraps around to the last person.

    • For example, if you are at the first person (index 0) and press "prev", it will wrap back to the last person (index 4).

Example:

  • If currentPersonIndex = 0 (person 1), then oldIndex - 1 = -1. Adding people.length gives us -1 + 5 = 4. When we apply % 5, we get 4 % 5 = 4, which takes us to the last person.

Key Points to Remember:

  • % people.length is what makes the carousel wrap around: going from the first to the last person, and vice versa.
  • The prevSlide function subtracts 1 to go backward, while nextSlide adds 1 to go forward.
  • Both functions update the currentPersonIndex, which determines which person is shown in the carousel.
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Create a Simple Carousel/Slider in React

Thematisch verwandte Begriffe: Create, Simple, CarouselSlider, React · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93977 | A vulnerability was determined in code-projects Assessment Management 1.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick