useLockBodyScroll

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

React 锁定在 body 元素上滚动的副作用钩子。适用于模态框和其他覆盖层组件。

Usage

  1. import {useLockBodyScroll, useToggle} from 'react-use';
  2. const Demo = () => {
  3. const [locked, toggleLocked] = useToggle(false)
  4. useLockBodyScroll(locked);
  5. return (
  6. <div>
  7. <button onClick={() => toggleLocked()}>
  8. {locked ? 'Unlock' : 'Lock'}
  9. </button>
  10. </div>
  11. );
  12. };

Reference

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