Coding Challenge Practice - Question 6

Today's Question Create a small React application that displays a series of slides and allows users to navigate through them. Solution First, the boilerplate provided: import React from "react"; function Slides({ slides }) { return ( Restart Prev Next "Title" "Text" ))} ); } export default Slides; The first step is to create a state to manage the slides. const [currentIndex, setCurrentIndex] = useState(0) The slide to be displayed will be determined by the index of the slide object in the array. The default slide on loading the application will be the first slide object. The index of the first item in an array is always 0. Next, the functions for the buttons used to navigate through the slides will be created. import React, { useState} from "react"; const handlePrev = () => { setCurrentIndex((prevIndex) => Math.max(prevIndex - 1, 0)) } const handleNext = () => { setCurrentIndex((prevIndex) => Math.min(prevIndex + 1, slides.length - 1)) } const handleRestart = () => { setCurrentIndex(0) } In setting the slide displayed to the previous slide, Math.max is used to ensure that the currentIndex cannot be less than 0. Likewise, Math.min is used to ensure that the currentIndex cannot be greater than the length of the slides array, i.e cannot be greater than the number of slides available. Finally, whatever the current slide is will be the only slide to be displayed. The h1 and p tags will be edited to display whatever values they have. const currentSlide = slides[currentIndex] return ( Restart Prev Next {currentSlide.title} {currentSlide.text} ); } export default Slides; To display the current slide, a variable is created that accepts the currentIndex as the index of the slide array. The final code looks like this: import React, { useState} from "react"; function Slides({ slides }) { const [currentIndex, setCurrentIndex] = useState(0); const handlePrev = () => { setCurrentIndex((prevIndex) => Math.max(prevIndex - 1, 0)) } const handleNext = () => { setCurrentIndex((prevIndex) => Math.min(prevIndex + 1, slides.length - 1)) } const handleRestart = () => { setCurrentIndex(0) } const currentSlide = slides[currentIndex] return ( Restart Prev Next {currentSlide.title} {currentSlide.text} ); } export default Slides; That's all folks!

May 12, 2025 - 00:33
 0
Coding Challenge Practice - Question 6

Today's Question

Create a small React application that displays a series of slides and allows users to navigate through them.

Solution

First, the boilerplate provided:

import React from "react";

function Slides({ slides }) {
  return (
    

"Title"

"Text"

))}
); } export default Slides;

The first step is to create a state to manage the slides.

const [currentIndex, setCurrentIndex] = useState(0)

The slide to be displayed will be determined by the index of the slide object in the array. The default slide on loading the application will be the first slide object. The index of the first item in an array is always 0.

Next, the functions for the buttons used to navigate through the slides will be created.

import React, { useState} from "react";


  const handlePrev = () => {
    setCurrentIndex((prevIndex) => Math.max(prevIndex - 1, 0))
  }
  const handleNext = () => {
    setCurrentIndex((prevIndex) => Math.min(prevIndex + 1, slides.length - 1))
  }
  const handleRestart = () => {
    setCurrentIndex(0)
  }

In setting the slide displayed to the previous slide, Math.max is used to ensure that the currentIndex cannot be less than 0. Likewise, Math.min is used to ensure that the currentIndex cannot be greater than the length of the slides array, i.e cannot be greater than the number of slides available.

Finally, whatever the current slide is will be the only slide to be displayed. The h1 and p tags will be edited to display whatever values they have.

  const currentSlide = slides[currentIndex]
  return (
    

{currentSlide.title}

{currentSlide.text}

); } export default Slides;

To display the current slide, a variable is created that accepts the currentIndex as the index of the slide array. The final code looks like this:

import React, { useState} from "react";

function Slides({ slides }) {
  const [currentIndex, setCurrentIndex] = useState(0);
  const handlePrev = () => {
    setCurrentIndex((prevIndex) => Math.max(prevIndex - 1, 0))
  }
  const handleNext = () => {
    setCurrentIndex((prevIndex) => Math.min(prevIndex + 1, slides.length - 1))
  }
  const handleRestart = () => {
    setCurrentIndex(0)
  }
  const currentSlide = slides[currentIndex]
  return (
    

{currentSlide.title}

{currentSlide.text}

); } export default Slides;

That's all folks!