A React hook to lock body scroll
npm install @kimhongyeon/use-lock-body-scrollA React hook to lock body scroll.
In many similar libraries, only the body overflow is set to hidden.
However, this approach may not work well in various situations. For example, when displaying a layer popup, the internal scroll should work while the body scroll should not.
This library not only sets the overflow to hidden but also sets the position to fixed, fundamentally preventing the body from moving. It also remembers the scroll position and restores it when the lock is released.
``bash`
npm install @kimhongyeon/use-lock-body-scroll`
Orbash`
yarn add @kimhongyeon/use-lock-body-scroll
jsx
import React, { useState } from 'react';
import { useLockBodyScroll } from '@kimhongyeon/use-lock-body-scroll';const App = () => {
const [isLocked, setIsLocked] = useState(false);
useLockBodyScroll(isLocked);
return (
);
};export default App;
``