react hook scroll left right on click code example
Example 1: toggle class onscroll hook react
const [scroll, setScroll] = useState(0)
useEffect(() => {
document.addEventListener("scroll", () => {
const scrollCheck = window.scrollY < 100
if (scrollCheck !== scroll) {
setScroll(scrollCheck)
}
})
})
Example 2: horizontal scroll onclick react
const scroll = (scrollOffset) => {
ref.current.scrollLeft += scrollOffset;
};
<Row>
<Col>
<button onClick={() => scroll(-20)}>LEFT</button>
<button onClick={() => scroll(20)}>RIGHT</button>
</Col>
</Row>