React.js:停止渲染从滚动到页面顶部 [英] React.js: Stop render from scrolling to top of page

查看:316
本文介绍了React.js:停止渲染从滚动到页面顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次在React.js中执行渲染时,UI都会滚动到页面顶部。

Every time you perform a render in React.js, the UI scrolls to the top of the page.

JSFiddle: http://jsfiddle.net/bengrunfeld / dcfy5xrd /

JSFiddle: http://jsfiddle.net/bengrunfeld/dcfy5xrd/

任何漂亮的或被动的方式来阻止它?

Any nifty or reactive way to stop that?

例如。如果用户向下滚动页面,然后按下导致渲染的按钮,UI将保持与之前相同的滚动位置。

E.g. If a User scrolls down the page, then pushes a button which causes a Render, the UI would stay in the same scroll location as before.

// Forces a render which scrolls to top of page
this.setState({data: data});

更新:为什么UI会滚动到顶部进行某些渲染,而不是其他渲染?

UPDATE: Why does the UI scroll to the top for some renders, but not others?

推荐答案

@floydophone在Twitter上回答了这个问题。

@floydophone answered this one on Twitter.

https://twitter.com/floydophone/status/608129119025561600

@bengrunfeld @reactjs你在链接处理程序上忘记了 preventDefault()

@bengrunfeld @reactjs you forgot preventDefault() on your link handlers

这篇关于React.js:停止渲染从滚动到页面顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆