Skip to content

Instantly share code, notes, and snippets.

@gaurangrshah
Created November 3, 2020 21:20
Show Gist options
  • Save gaurangrshah/d602a07c42e4a8eb96b5ef1ce7610a62 to your computer and use it in GitHub Desktop.
Save gaurangrshah/d602a07c42e4a8eb96b5ef1ce7610a62 to your computer and use it in GitHub Desktop.
#react #hooks
import { useLayoutEffect } from "react"
export const useBodyScrolllock = toggle => {
useLayoutEffect(() => {
let originalStyle
let lockScroll = !toggle ? false : true
if (lockScroll) {
originalStyle = window.getComputedStyle(document.body).overflow
document.body.style.overflow = "hidden"
}
return () => (document.body.style.overflow = originalStyle)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment