I am stuck on an issue with my SlideShow with Redux , which I am new with. I am trying to figure out the logic to connect my dropdown select and the next and previous buttons so that redux keeps track of my state. I want the user to be able to navigate through the slides, and also be able to select a slide from the dropdown. I cant figure out what I should do to connect it all but i think i'm close... :\
My Slides
const SlideData = [
{
title: "Introduction",
content: "This is some content",
},
{
title: "Slide 2",
content: "This is some content",
},
{
title: "Slide 3",
content: "This is some content",
},
{
title: "Slide 4",
content: "This is some content",
},
];
export default SlideData;
REDUX FILE
import { createSlice, configureStore } from "@reduxjs/toolkit";
import SlideData from "../SlideData";
//SET INITIAL STATE//
const initialSlideState = {
SlideData,
totalSlides: SlideData.length,
currentSlide: 0,
progress: 0,
};
//SET REDUCERS//
const slideSlice = createSlice({
name: "slide",
initialState: initialSlideState,
reducers: {
setCurrentSlide(state, action) {
state.currentSlide = SlideData[action.payload];
},
nextSlide(state) {
if (state.currentSlide < state.totalSlides) {
state.currentSlide++;
}
},
prevSlide(state) {
if (state.currentSlide > 0) {
state.currentSlide--;
}
},
setProgress(state, action) {
state.progress = action.payload;
},
},
});
//CREATE STORE//
const store = configureStore({
reducer: slideSlice.reducer,
});
export const slideActions = slideSlice.actions;
export default store;
SLIDESHOW FILE
//IMPORTS//
import React, { useRef } from "react";
import styles from "./Slideshow.module.css";
import Button from "./UI/Button";
import { Slide } from "react-slideshow-image";
import SlideData from "../SlideData";
import { useSelector, useDispatch } from "react-redux";
import { slideActions } from "../store/index";
import "react-slideshow-image/dist/styles.css";
export default function Slideshow() {
const dispatch = useDispatch();
const slideRef = useRef();
const currentSlide = useSelector((state) => state.currentSlide);
const totalSlides = useSelector((state) => state.totalSlides);
//SET CURRENT SLIDE
const setCurrentSlideHandler = (index) => {
dispatch(slideActions.setCurrentSlide(index));
};
//NEXT SLIDE
const nextSlideHandler = () => {
dispatch(slideActions.nextSlide());
dispatch(slideActions.setProgress((currentSlide / totalSlides) * 100));
slideRef.current.goNext();
};
//PREVIOUS SLIDE
const prevSlideHandler = () => {
dispatch(slideActions.prevSlide());
dispatch(slideActions.setProgress((currentSlide / totalSlides) * 100));
slideRef.current.goBack();
};
//GO TO SLIDE (DROPDOWN)<<--feel like i'm close here?
const goto = ({ target }) => {
console.log("Before: " + currentSlide);
let info = parseInt(target.value, 10);
console.log(info);
setCurrentSlideHandler(info);
slideRef.current.goTo(currentSlide);
console.log("after: " + currentSlide);
};
//SLIDESHOW PROPS//
const properties = {
transitionDuration: 200,
autoplay: false,
arrows: false,
};
//MAP DATA FOR DROPDOWN//
const options = SlideData.map((item, index) => (
<option key={index} value={index}>
{item.title}
</option>
));
return (
<div className={styles.container}>
<Slide ref={slideRef} {...properties}>
{SlideData.map((item, index) => (
<div key={index} className={styles.slide}>
<h2>{item.title}</h2>
<p>{item.content}</p>
</div>
))}
</Slide>
<div>
<Button type="button" onClick={prevSlideHandler}>
Back
</Button>
<Button type="button" onClick={nextSlideHandler}>
Next
</Button>
<select className={styles.select} onChange={goto}>
<option>--Select--</option>
{options}
</select>
</div>
</div>
);
}