useLockBodyScroll

Prevent scrolling of the body element.

https://www.shaktools.com/shak-hooks
Browser

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useLockBodyScroll } from "@shak-hooks/usehooks";
Source: packages/react/src/useLockBodyScroll.ts

Signature

signature.ts
export function useLockBodyScroll(locked: boolean = true)

Parameters

NameTypeOptionalDefault
lockedbooleanYestrue

Returns

unknown

Implementation

use-lock-body-scroll.ts
1import { useLayoutEffect } from "react";
2
3export function useLockBodyScroll() {
4 useLayoutEffect(() => {
5 const originalStyle = window.getComputedStyle(document.body).overflow;
6 document.body.style.overflow = "hidden";
7 return () => {
8 document.body.style.overflow = originalStyle;
9 };
10 }, []);
11}

Advertisement

Google Ads

Usage

example.tsx
1import { useLockBodyScroll } from "@shak-hooks/usehooks";
2
3const result = useLockBodyScroll(undefined);
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy