Top React Hooks — Lock Scrolling, Animation Loop, Session Storage, and Throttling

Photo by Anders Wideskott on Unsplash

Hooks contains our logic code in our React app.

We can create our own hooks and use hooks provided by other people.

In this article, we’ll look at some useful React hooks.


The react-use library is a big library with many handy hooks.


The useLockBodyScroll hook lets us lock the scrolling of the body element.

It’s useful for modals and overlays.

It takes an argument to let us lock scrolling or not.

For example, we can write:

import React from "react";
import { useLockBodyScroll, useToggle } from "react-use";
export default function App() {
const [locked, toggleLocked] = useToggle(false);
useLockBodyScroll(locked); return (
<button onClick={() => toggleLocked()}>
{locked ? "Unlock" : "Lock"}
.map((_, i) => (

We have a button to toggle the locked state.

If it’s true , we disable locking scrolling on the body.

Otherwise, we let users scroll the body.


The useRafLoop hook lets us run the requestAnimationFrame loop without re-rendering the parent component.

The loop stops automatically on component unmount.

For instance, we can write:

import React from "react";
import { useRafLoop, useUpdate } from "react-use";
export default function App() {
const [ticks, setTicks] = React.useState(0);
const [lastCall, setLastCall] = React.useState(0);
const update = useUpdate();
const [loopStop, loopStart, isActive] = useRafLoop(time => {
setTicks(ticks => ticks + 1);
return (
<div>RAF triggered: {ticks} (times)</div>
<div>Last high res timestamp: {lastCall}</div>
<br />
onClick={() => {
isActive() ? loopStop() : loopStart();
{isActive() ? "stop" : "start"}

We use the useRafLoop hook by passing in callback with the time , which is the timestamp since the loop started.

The timestamp is in milliseconds.

It returns an array with the loopStop , loopStart and isActive variables.

loopStop lets us stop the requestAnimationFrame loop.

loopStart lets us start the requestAnimationFrame loop.

isActive is true when the requestAnimationFrame loop is active.


The useSessionStorage hook lets us manage our browser’s session storage.

To use it, we write:

import React from "react";
import { useSessionStorage } from "react-use";
export default function App() {
const [value, setValue] = useSessionStorage("session-key", "foo");
return (
<div>Value: {value}</div>
<button onClick={() => setValue("qux")}>qux</button>
<button onClick={() => setValue("baz")}>baz</button>

The useSessionStorage hook takes the key for the session storage as the first argument.

The 2nd argument is the value for the given key.

It returns the value and setValue variables to let us get and set the value respectively.


We can use the useThrottle hook to throttle the value updates.

To use it, we can write:

import React from "react";
import { useThrottle } from "react-use";
export default function App() {
const [value, setValue] = React.useState(0);
const throttledValue = useThrottle(value, 1000);
return (
<button onClick={() => setValue(c => c + 1)}>increment</button>
<div>Value: {value}</div>
<div>Throttled value: {throttledValue}</div>

We call the useThrottle hook, which takes the state value and the number of milliseconds to delay the updates.

Then we render both the unthrottled and throttled values.

Photo by Ira Mint on Unsplash


We can throttle state updates, lock scrolling, run the requestAnimationFrame loop, and manage session storage with the react-use library.

Web developer. Subscribe to my email list now at Email me at

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store