I am trying to access the horizontal scroll bar with buttons in react but turns out it is not working because of error that cannot read the null values of .scrollLeft.
<>
<div className="containerOuterSider">
<FaAngleLeft className= "FaAngleLeft" onClick={slide('left')}/>
<div id="container3" className="container3">
{products &&
products
.map((product) => (
<AAsOfLowNav key={product._id} product={product} />
))
.reverse()}
</div>
<FaAngleRight className="FaAngleRight" onClick={slide('right')}/>
</div>
</>
and the main function is
function slide(direction) {
var container = document.getElementById('container3');
let scrollCompleted = 0;
var slideVar = setInterval(function() {
if (direction === 'left') {
container.scrollLeft -= 10;
} else {
container.scrollLeft += 10;
}
scrollCompleted += 10;
if (scrollCompleted >= 100) {
window.clearInterval(slideVar);
}
}, 50);
}
The error I am facing is:
Uncaught TypeError: Cannot read properties of null (reading 'scrollLeft') at LowerCatNav.js:31:1 Uncaught TypeError: Cannot read properties of null (reading 'scrollLeft') at LowerCatNav.js:33:1
How can I make it right? Should I use the hooks or anything else?