Understand the code implementation of useSessionStorage custom hook in react that will help to efficiently manager session storage in application.
Anuj Sharma
Last Updated Nov 15, 2025
![Implement useSessionStorage() Custom Hook in React [Interview]](/_next/image?url=https%3A%2F%2Ftwijvresuxjxyhisasvd.supabase.co%2Fstorage%2Fv1%2Fobject%2Fpublic%2Fblog-images%2F5bf882d3-0967-44f5-bb29-5f33706cdfa6%2Fpattern-1763207973827-421m1d.png&w=3840&q=75&dpl=dpl_Cv68KV2nkEPH27CnEK1BB1odsK4t)
useLocalStorage and useSessionStorage is most widely used custom hooks to simplify the interaction with localStorage and sessionStorage at the application level and provide a clean way for interaction.
In this blog, Let's understand the implementation code of useSessionStorage custom hook in react.
While exploring the approach, its important to know that useSessionStorage custom hook in react will internally going to use the window.sessionStorage object only, and convert the object into string using JSON.stringify() function.
As part of the custom hook, we need to handle the set and get the session storage values, and introduce error handing at the common place under useSessionStorage custom hook, to handle any runtime errors.
Let's checkout the code implementation and explanation of the useSessionStorage hook.
import { useState } from 'react';
const useSessionStorage = (key, initialValue) => {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.sessionStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.error(error);
return initialValue;
}
});
const setValue = (value) => {
try {
setStoredValue(value);
window.sessionStorage.setItem(key, JSON.stringify(value));
} catch (error) {
console.error(error);
}
};
return [storedValue, setValue];
};
export default useSessionStorage;
Let's understand the useSessionStorage usage with a simple example of setting the useName key in the session storage.
import React from 'react';
import useSessionStorage from './useSessionStorage';
const App = () => {
const [name, setName] = useSessionStorage('userName', 'Guest');
const handleNameChange = (e) => {
setName(e.target.value);
};
return (
<div>
<input type="text" value={name} onChange={handleNameChange} />
<p>Hello, {name}!</p>
</div>
);
};
export default App;
In this example, we import the useSessionStorage custom hook and use it to manage the 'userName' key in sessionStorage with a default value of 'Guest'. We then update the name value based on user input in an input field.
Anuj Sharma
A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me 🚀
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Build Your Portfolio
Help the Community
Strengthen Your Skills
Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀
Anuj Sharma
Last Updated Dec 23, 2025
Explore code explanation of useToggle() custom hook in react to handle the toggle event efficiently.
Anuj Sharma
Last Updated Oct 2, 2025
Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Anuj Sharma
Last Updated Nov 24, 2025
Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Nov 23, 2025
Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2025 FrontendGeek. All rights reserved