ScrollIntoView in React without refs?

谁说我不能喝 提交于 2021-01-29 06:10:04

问题


I have a big list of items / divs that I need to create programmatically, and I need to implement scrollIntoView buttons for each them. I know how to do it with refs.

Whether there is an alternative to refs that might be more performant?


回答1:


I believe something like this will work:

onScrollClick(ev) {
    ev.target.scrollIntoView();
}

render() {
    return (
           ... <button onClick={this.onScrollClick}>Scroll This Element Into View</button>
      )
}

That assumes you want to scroll the button itself into view. If that's not what you want, you'll have to be specific.

[edit] if it's not the element itself, but one of the elements parents, you can also find a parent with javascript apis, element.parentElement -- you can use that as many times as you need to find the relevant element.



来源:https://stackoverflow.com/questions/60439016/scrollintoview-in-react-without-refs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!