I am new to react and currently I am trying to set the scrollTop property of a div to a desired number. Unfortunately, it doesn't work for me. See the code below:
class Testdiv extends React.Component {
constructor(props){
super(props);
this.divRef = React.createRef();
}
componentDidMount(){
this.divRef.current.scrollTop = 100;
}
render(){
return (
<div className="testDiv" ref={this.divRef}>
Hello World<br /><br /><br /><br />
Hello World!
</div>
);
}
}
ReactDOM.render(
<Testdiv />,
document.getElementById('root')
);
And the CSS:
.testDiv{
height: 500px;
overflow: auto;
}
Here is the codeio pen for the respective code. P.S. I don't want to use Jquery. Thank you for the help.