Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to scroll to an element?

I have a chat widget that pulls up an array of messages each time I look up. The difficulty I am confronting now is the slider stays fixed at the top when messages load. I need it to focus on the last index element from the past array. I sorted out that I can make dynamic refs bypassing index, however, I would likewise have to understand what sort of scroll function to use to accomplish that

handleScrollToElement(event) {
const tesNode = ReactDOM.findDOMNode(this.refs.test)
if (some_logic){
//scroll to testNode
}
}

render() {

return (
<div>
<div ref="test"></div>
</div>)
}
by

2 Answers

rahul07

const scrollTo = (ref) => {
if (ref / + other conditions /) {
ref.scrollIntoView({ behavior: 'smooth', block: 'start' })
}
}

<div ref={scrollTo}>Item</div>
kshitijrana14
You can also use scrollIntoView method to scroll to a given element.
handleScrollToElement(event) {
const tesNode = ReactDOM.findDOMNode(this.refs.test)
if (some_logic){
tesNode.scrollIntoView();
}
}

render() {
return (
<div>
<div ref="test"></div>
</div>)
}

Login / Signup to Answer the Question.