Ok, I am trying to create a dropdown and when selecting an item, the page should scroll to a specific section (ref).
I have several divs with the same ref (as I have more than 30 divs that require a ref).
const SettingView = () => {
const selectedItem = "el2"; // This is temporarily as it will be the item from a dropdown
const ref = useRef<HTMLDivElement[]>([]);
const filterRef = (el: HTMLDivElement) => ref.current.push(el);
return (
<>
<div ref={filterRef} id="el1">Some Text</div>
<div ref={filterRef} id="el2">Some Text2</div>
<div ref={filterRef} id="el3">Some Text3</div>
<div ref={filterRef} id="el4">Some Text4</div>
....
</>
)
}
export default SettingView;
So on a button click it should find the div ref that has the id from the selectedItem and scroll to it.
How would I do that?
2
Answers
You can achieve this by using the scrollIntoView() method on the selected element’s ref. Here’s how you can modify your SettingView component to achieve this functionality:
In this code:
scrollToRef function takes the ref array and the id of the selected item. It finds the ref with the matching id and scrolls it into view using scrollIntoView() with options for smooth behavior and starting from the block’s start.
useEffect hook is used to trigger the scrolling effect whenever selectedItem changes.
If you render your buttons using a
map()
, which should be fine since only the id differs, then you can use the iterator ofmap
to use that as an id.Then when clicking the button, get that index in the
ref.current
array and call (eg)scrollIntoView()
on that.Example:
scrollIntoView
on thatscrollIntoView
on index50