<div key={"parent-div"}>
<div key={"child-div-1"}></div>
<div key={"child-div-2"}></div>
<div key={"child-div-3"}></div>
<div key={"child-div-4"}></div>
<div key={"child-div-5"}></div>
</div>
The above React component will overflow the viewpoint.
Task: before the component is rendered on the screen, I want child-div-3
to be scrolled to the top.
If the parent div is rendered on the screen first and then the scrolling takes place, then there will be a slight visual glitch. This is what I want to prevent.
<div key={"parent-div"}>
<div key={"child-div-1"}></div>
<div key={"child-div-2"}></div>
<div key={"child-div-3"}></div>
<div key={"child-div-4"}></div>
<div key={"child-div-5"}></div>
</div>
The above React component will overflow the viewpoint.
Task: before the component is rendered on the screen, I want child-div-3
to be scrolled to the top.
If the parent div is rendered on the screen first and then the scrolling takes place, then there will be a slight visual glitch. This is what I want to prevent.
Share Improve this question edited Feb 4 at 4:46 Phil 165k25 gold badges262 silver badges267 bronze badges asked Feb 4 at 4:32 Bear Bile Farming is TortureBear Bile Farming is Torture 5,1318 gold badges41 silver badges118 bronze badges1 Answer
Reset to default 3You want to run something before the screen is actually painted, but you need the result of rendering before you do this. You can use useLayoutEffect
.
- Create a ref using
useRef
pointing to the div. - Use
scrollIntoView
to scroll to the div in an effect. (Do this before paint)
You can also create a hook for this:
const useScrollToDivTop = () => {
const divRef = useRef();
useLayoutEffect(() => {
divRef.current.scrollIntoView();
}, []);
return { divRef };
};
export default function App() {
const { divRef } = useScrollToDivTop();
return (
<div key={"parent-div"} className="parent-div">
<div key={"child-div-1"}>1</div>
<div key={"child-div-2"}>2</div>
<div ref={divRef} key={"child-div-3"}>
3
</div>
<div key={"child-div-4"}>4</div>
<div key={"child-div-5"}>5</div>
</div>
);
}
Sandbox