-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Closed
Description
First, thank you for creating and maintaining this incredibly useful library.
I'm having trouble getting scroll position to reset to the top of a page on navigation in RC1. My code looks something like the following:
var indexComponent = React.createClass({
render() {
return (
<div>
{this.children}
</div>
)
}
});
var componentOne = React.createClass({
mixins: [History],
handleClick() {
this.history.pushState(null, '/route-two')
},
render() {
return (
<div>
<a onClick={this.handleClick}>
Link to Component 2
</a>
</div>
)
}
});
var componentTwo = React.createClass({
mixins: [History],
handleClick() {
this.history.pushState(null, '/route-one')
},
render() {
return (
<div>
<a onClick={this.handleClick}>
Link to Component 1
</a>
</div>
)
}
});
var routeConfiguration = {
path: "/",
indexRoute: {
component: indexComponent
},
childRoutes: [
{
path: "route-one",
component: componentOne,
onEnter: doSomeAuthStuff,
ignoreScrollBehavior: true
},
{
path: "route-two",
component: componentTwo,
onEnter: doSomeAuthStuff,
ignoreScrollBehavior: true
}
]
};
React.render(
<Router history={browserHistory} routes={routeConfiguration} />,
document.getElementById('app')
);
Am I using the wrong part of the API to reset scroll position to the top of the page? If so, what's the proper way to trigger that behavior?
Activity
kaikuchn commentedon Sep 30, 2015
See #1958 However, setting the scroll position
onUpdate
as suggested didn't work for me for no comprehensible reason. I had to put it in theonEnter
hook (which isn't nice but works for my case) :/knowbody commentedon Sep 30, 2015
as @kaikuchn mentioned, use
onUpdate
:nicolashery commentedon Oct 25, 2015
Using
onUpdate
didn't quite work out for me because I wanted to "preserve scroll position when using the back button, scroll up when you come to a new page".Maybe
onUpdate
should havelocation
passed to its arguments?The workaround I found was this:
kjs3 commentedon Nov 4, 2015
Here's my slightly modified hack to scroll to a URL fragment if it exists
taion commentedon Nov 4, 2015
@kjs3 We're actively discussing this on #2471 and the associated issue. Would appreciate your feedback there since you've obviously put some thought into this.
kjs3 commentedon Nov 4, 2015
@taion Ok cool, I'll test it out and let you know over there.
Scroll to top when entering new page, preserve scroll when using brow…