Next.js starter your AI actually understands. Ship internal tools in days not weeks. Pre-order $199 $499 → [Get it now]

How to persist state with sessionStorage in React

Using sessionStorage for state persistence is ideal when you need temporary data storage that clears when the browser tab closes. As the creator of CoreUI with extensive React development experience since 2014, I’ve used sessionStorage for form data, wizard steps, and temporary user preferences. The implementation is nearly identical to localStorage but provides session-scoped persistence instead of permanent storage. This approach is perfect for sensitive data or temporary application state that shouldn’t persist indefinitely.

Use useEffect with sessionStorage to persist state temporarily within the current browser session.

import { useState, useEffect } from 'react'

function FormWizard() {
    const [step, setStep] = useState(() => {
        const saved = sessionStorage.getItem('wizardStep')
        return saved ? parseInt(saved, 10) : 1
    })

    useEffect(() => {
        sessionStorage.setItem('wizardStep', step.toString())
    }, [step])

    return (
        <div>
            <p>Current Step: {step}</p>
            <button onClick={() => setStep(step + 1)}>
                Next Step
            </button>
        </div>
    )
}

This code persists the wizard step using sessionStorage, which automatically clears when the browser tab is closed. The lazy initial state function retrieves the saved step on component mount, while useEffect saves the current step whenever it changes. Unlike localStorage, this data won’t survive browser restarts, making it perfect for temporary workflow state.

Best Practice Note:

This is the approach we use in CoreUI multi-step forms where temporary persistence improves user experience without cluttering permanent storage. SessionStorage is ideal for wizard flows, temporary drafts, and any data that should reset with each new session.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
Dealing with Sass Deprecation Warnings in Angular 19
Dealing with Sass Deprecation Warnings in Angular 19

How to Remove Underline from Link in CSS
How to Remove Underline from Link in CSS

How to Center a Button in CSS
How to Center a Button in CSS

How to Disable Right Click on a Website Using JavaScript
How to Disable Right Click on a Website Using JavaScript

Answers by CoreUI Core Team