react-use/docs/useLockBodyScroll.md
2019-04-09 19:06:09 +03:00

621 B

useLockBodyScroll

React side-effect hook that locks scrolling on the body element. Useful for modal and other overlay components.

Usage

import {useLockBodyScroll, useToggle} from 'react-use';

const Demo = () => {
  const [locked, toggleLocked] = useToggle(false)

  useLockBodyScroll(locked);

  return (
    <div>
      <button onClick={() => toggleLocked()}>
        {locked ? 'Unlock' : 'Lock'}
      </button>
    </div>
  );
};

Reference

useLockBodyScroll(enabled?: boolean = true);
  • enabled — Hook will lock scrolling on the body element if true, defaults to true