Skip to content
Advertisement

cant change the backgroud after updating the hook

i have a hook that holds number (as index) and array of images:

JavaScript

i made two buttons that hold onClick event that should change the background to another image (by index):

JavaScript

and the functions:

JavaScript

by this the hook (currentIndex) should change and display the next image but its not working. what am i missing?

this is the style for the last div:

JavaScript

Advertisement

Answer

I think maybe you might just have a couple of bugs in the logic you’re using to find the new index in each slide function.

In the slideToLeft function you might want to set the index to one less than the current index?

JavaScript

Then you might have a similar problem in the slideToRight function – I’m guessing you just want to do your math on the length of the starsOfTheWeek array?

JavaScript

Lastly, I can’t see what else might be causing this component to render but you might want to consider wrapping these functions inside of useCallback and passing in the necessary dependencies, something like:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement