I am trying to access a specific point on DOM using useRef() from a dropdown.
at the moment I have implemented the logic using # ID but I want to use useRef().
Also to be mentioned: I don't have access to the App.js to
pass the ref to other components.
dropdown.js
export default function DropDown() {
const ScrollToTitle = (e) => {
const title = e.target.value;
window.location = '#' + title;
// how to access the ref in here insted of # ID?
};
return (
<select onChange={ScrollToTitle}>
<option>First </option>
<option>Second </option>
<option>Third </option>
<option>Another </option>
<option>Somthing </option>
</select>
);
}
I have a couple of titles on the App but as an example here is one of them:
export default function First() {
const titleRef = useRef(); // what I wan to use
return (
<div id='First'>
<h1 ref={titleRef}>First </h1> // is this correct ?
<p>Start editing to see some magic happen :)</p>
<p>Start editing to see some magic happen :)</p>
</div>
);
}
How and from where to access the titleRef?
to be mentioned again : I don't have access to the App.js to
pass the ref to other components.
and I have a live example here: https://stackblitz.com/edit/react-cpezfb
Thanks a lot