I am trying to create a list in a React app, which the user can append items to. This list is in a div with a fixed height, so after some items, the user has to scroll. The behaviour I want to achieve is that when the user adds a new item to the list, the list scrolls to the end. What always happens, regardless of which solution I try to use, is that the whole page scrolls as well. I want the page to stay exactly where it is - the list itself should not move - and only the list div to scroll to its end.
This is my minimal working example, if you put this in a scrollable page that is.
const Test = () => {
const [num, setNum] = useState([0, 1, 2]);
const last = useRef(null);
useEffect(() => {
last.current.scrollIntoView({ behavior: "smooth" });
}, [num]);
return (
<div
style={{
overflow: "auto",
width: "100%",
height: 300,
background: "green",
flexDirection: "column",
justifyContent: "flex-start",
}}>
{num.map((numer) => (
<div key={numer} style={{ height: 20, margin: 2, color: "blue" }}>
{numer}
</div>
))}
<button
ref={last}
onClick={() => {
setNum([...num, num.slice(-1)[0] + 1]);
}}>
Add number
</button>
</div>
);
};