Skip to content

Instantly share code, notes, and snippets.

@kristoferbaxter
Forked from ryanflorence/ManageScroll.js
Last active August 13, 2018 21:58
Show Gist options
  • Save kristoferbaxter/a423a64d0d2a7634daee6eac5188ee60 to your computer and use it in GitHub Desktop.
Save kristoferbaxter/a423a64d0d2a7634daee6eac5188ee60 to your computer and use it in GitHub Desktop.
import React from "react";
import { Location } from "@reach/router";
let scrollPositions = {};
class ManageScrollImpl extends React.Component {
componentWillUnmount() {
scrollPositions[this.props.location.key] = window.scrollY;
}
componentDidUpdate() {
window.scrollTo(0, scrollPositions[this.props.location.key] || 0);
}
render() {
return null;
}
}
export default () => (
<Location>
{({ location }) => <ManageScrollImpl location={location} />}
</Location>
);
@kristoferbaxter
Copy link
Author

Fork from here. Created to ask a question.

@kristoferbaxter
Copy link
Author

Answered here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment