When the button is clicked, my list is replaced with a one-item list. When I click on the “return to list” button, I want to return to the same list and to the same place in the list to which I scrolled.
useEffect(() => {
if (props.specialOfferRestaurant) {
localStorage.setItem('scrollPosition', String(window.scrollY))
localStorage.setItem('restaurants', JSON.stringify(allRestaurants));
window.scrollTo(0, 0)
setAllRestaurants([props.specialOfferRestaurant])
} else if (!props.specialOfferRestaurant && localStorage.length > 0) {
setAllRestaurants(JSON.parse(localStorage.getItem('restaurants')))
window.scrollTo(0, Number(localStorage.getItem('scrollPosition')))
localStorage.clear();
}
}, [props.specialOfferRestaurant])
In this code, everything works except for the line that returns the scroll to the right place. The same line that returns the scroll to 0 also works. I can’t figure out what’s wrong.