Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@eliumhq/react-router-restore-scroll
Advanced tools
Scroll positions restoration for React Router
Restore the scroll positions of window
and scrollable elements when
the user navigates around a React Router app.
Plan is to put this into React Router directly, but for now you can plug it in and help us get the bugs out (and write some tests, there aren't any yet!)
npm install react-router-restore-scroll
import React from 'react'
import { render } from 'react-dom'
import { Router, browserHistory, applyRouterMiddleware } from 'react-router'
import routes from './routes'
import {
useHistoryRestoreScroll,
useRouterRestoreScroll
} from 'react-router-restore-scroll'
// first enhance a history
const createHistory = useHistoryRestoreScroll(() => browserHistory)
// next create some router middleware
const routerRender = applyRouterMiddleware(
useRouterRestoreScroll()
)
// then plug them into Router
render(
<Router
history={createHistory()}
render={routerRender}
routes={routes}
/>,
document.getElementById('app')
)
Now the window's scroll positions will be automatically restored as you navigate around in a React Router app, and even when you navigate out of and back into it from external sites.
If you’ve got scrollable elements (overflow: auto|scroll
) they can
also be restored with the RestoreScroll
component.
import { RestoreScroll } from 'react-router-restore-scroll'
// then in a component's render method, wrap your scrollable element
// in a `RestoreScroll` component. It needs a `scrollKey`.
<RestoreScroll scrollKey="one">
<div style={{ height: '200px', overflow: 'auto', border: '1px solid' }}>
<div style={{ height: '100px', background: 'hsl(0, 50%, 90%)' }}>scroll me</div>
<div style={{ height: '100px', background: 'hsl(100, 50%, 90%)' }}>two</div>
<div style={{ height: '100px', background: 'hsl(200, 50%, 90%)' }}>three</div>
</div>
</RestoreScroll>
The useHistoryRestoreScroll
enhancer gives you a history with a
restoreScroll
property with three methods that you can use to
integrate into any view layer that uses history
(cycle.js, etc.).
import createBrowserHistory from 'history/lib/createBrowserHistory'
import useHistoryRestoreScroll from 'react-router-restore-scroll/lib/useHistoryRestoreScroll'
const history = useHistoryRestoreScroll(createBrowserHistory)()
// call this when a scrollable element is inserted into the dom, where
// `scrollKey` is a globally unique identifier for the node
history.restoreScroll.registerScroller(scrollKey, domNode)
// call this when it's removed from the dom
history.restoreScroll.unregisterScroller(scrollKey)
// call this
// - when the app first renders
// - after the app re-renders after a location change
history.restoreScroll.restoreWindow()
You can look at modules/RestoreWindowScroll.js
and modules/RestoreScroll.js
to see at which points in a React app these methods are all called.
We'll pull useHistoryRestoreScroll
out into it's own package on npm
eventually so that this use-case isn't required to bring in stuff that
depends on React and React Router.
location.key
in preparation for history 3.0shouldUpdateScroll
, but once route is on
context we'll be
able to implement the old ignoreScroll
route prop or maybe a function like shouldUpdateScroll
instead.FAQs
Scroll positions restoration for React Router
We found that @eliumhq/react-router-restore-scroll demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.