<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq" -->

---
title: 30 seconds of code | daily.dev
description: Use the useLayoutEffect() with an empty array as the second argument to execute the provided callback only once when the component is mounted. Use...
canonical: https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: 30 seconds of code | daily.dev
og:description: Use the useLayoutEffect() with an empty array as the second argument to execute the provided callback only once when the component is mounted. Use...
og:url: https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq
og:image: https://api.daily.dev/og/posts/cDB3ckfhq.png
og:image:alt: 30 seconds of code
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 30 seconds of code

**[30 seconds of code](https://daily.dev/sources/30seconds)** · 1 min read · 19 upvotes · 2 comments

## Summary

Use the useLayoutEffect() with an empty array as the second argument to execute the provided callback only once when the component is mounted. Use Window.getComputedStyle() to get the overflow value of the body element and store it in a variable. Enables body scroll locking.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.30secondsofcode.org/react/s/use-body-scroll-lock>

## Community discussion

Top comments from developers on daily.dev.

**@horace\_torvalds** · 0 upvotes

> i mean where was i not knowing this site

**@lorezz** · 0 upvotes

> Great stuff as always with 30 sec of code.
> Lovely

---

Tags: [#general-programming](https://daily.dev/tags/general-programming)

[View this post on daily.dev](https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"30 seconds of code","url":"https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq"},"datePublished":"2021-09-04T10:43:12.061Z","dateModified":"2021-10-07T14:17:10.317Z","description":"Use the useLayoutEffect() with an empty array as the second argument to execute the provided callback only once when the component is mounted. Use...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/43d23aa392d8c0e2d94bc3932fe189ec","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/43d23aa392d8c0e2d94bc3932fe189ec","isAccessibleForFree":true,"articleSection":"30 seconds of code","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"30 seconds of code","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89","url":"https://daily.dev/sources/30seconds"},"commentCount":2,"discussionUrl":"https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":19},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"keywords":"general-programming","timeRequired":"PT1M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"30 seconds of code","item":"https://daily.dev/sources/30seconds"},{"@type":"ListItem","position":3,"name":"30 seconds of code"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq","comment":[{"@type":"Comment","text":"i mean where was i not knowing this site","datePublished":"2021-09-04T20:48:55.116Z","url":"https://daily.dev/posts/cDB3ckfhq#c-uafvPJMSt","author":{"@type":"Person","name":"Horace","url":"https://daily.dev/horace_torvalds","image":"https://avatars3.githubusercontent.com/u/50598183?v=4"}},{"@type":"Comment","text":"Great stuff as always with 30 sec of code.\nLovely","datePublished":"2021-12-02T19:16:38.719Z","url":"https://daily.dev/posts/cDB3ckfhq#c-g2jJH4Rg8","author":{"@type":"Person","name":"lorezz","url":"https://daily.dev/lorezz","image":"https://avatars0.githubusercontent.com/u/1153066?v=4"}}]}
```

