Skip to content
πŸ”’ Prevent scroll on the <body />
Branch: master
Clone or download

React Scroll Lock

Prevent scroll on the <body /> when a component is mounted.


yarn add react-scrolllock


import ScrollLock, { TouchScrollable } from 'react-scrolllock';

class Modal extends Component {
  state = { lockScroll: false }
  render() {
    return (
        // the lock accepts a single child element, which supports touch-scrolling.

        // if your app structure doesn't allow wrapping like above, the `TouchScrollable`
        // component is exposed as a named export.
        <ScrollLock />
        // you can also toggle the lock based on some state.
        <ScrollLock isActive={this.state.lockScroll} />



Property Description
accountForScrollbars boolean Default: true -- Whether or not to replace the scrollbar width when active.
isActive boolean Default: true -- Whether or not the lock is active.
children element Default: null -- This element is wrapped internally by the TouchScrollable component.


Wrap an element in the TouchScrollable component if you need an area that supports scroll on mobile.

This is necessary because the touchmove event is explicitly cancelled β€” iOS doesn't observe overflow: hidden; when applied to the <body /> element 😒

Property Description
children element Required The element that can be scrolled.
You can’t perform that action at this time.