React Use Window.sessionStorage
Edit page
Home
Hooks
useClearSessionStorageuseSessionStorageBooleanuseSessionStorageItemuseSessionStorageNumberuseSessionStorageObjectuseSessionStorageStringDescriptionUsageSignatureExample
Code of ConductMIT License

useSessionStorageString

Description

This hook gets and sets a string in sessionStorage.

Features synchronization across hooks sharing the same key name.

Usage

import { useSessionStorageString } from 'react-use-window-sessionstorage';

In your function component:

const [value, setValue] = useSessionStorageString('favColor', 'cyan');

The hook takes a key name and an optional default value argument. The default value will be immediately written to sessionStorage and returned if no value is present already. Uses null when no default value is provided.

Use value and setValue to read and write the value.

Additional Features

The hook provides additional features in its return array, should you be interested:

const [value, setValue, loading, available, reset] = useSessionStorageString('favColor', 'cyan');
  • A loading value of true indicates that the value is being loaded from sessionStorage.
  • An available value of true indicates that sessionStorage is supported and available for use.
  • The reset() function sets the value back to the provided default, or null if none was given.

Signature

useSessionStorageString(keyName: string, defaultValue?: string | null) => [string | null, (value: string | null) => void, boolean, () => void, () => void]

Props

PropTypeDescription
keyNamestringRequired. Key name to use in sessionStorage.
defaultValuestring | nullOptional. Provide a default string value when the key's value is not found in sessionStorage. Will be immediately written to sessionStorage if not present. Use null for no default.

Return

The hook returns an array containing:

IndexNameTypeDescription
0valuestring | nullThe string value from sessionStorage, when loading is false. Will be the default value (or null if none provided) while loading is true. If null when not loading, the key was not found in sessionStorage.
1setValue(value: string | null) => voidSets the value in sessionStorage. Provide null to set it back to the default value. Providing null will delete the key from sessionStorage if the default value is null.
2loadingbooleantrue while the value loads from sessionStorage, false otherwise.
3availablebooleantrue if sessionStorage is supported and available for use, false otherwise.
4reset() => voidReset the value to the default.

Example


import * as React from 'react';
import { useSessionStorageString } from 'react-use-window-sessionstorage';
export function UseSessionStorageStringExample() {
const defaultValue = 'Zelda';
const [enteredValue, setEnteredValue] = React.useState('');
const [value, setValue, loading, available, reset] = useSessionStorageString('stringValue', defaultValue);
const [value2, setValue2, loading2] = useSessionStorageString('stringValue', defaultValue);
return (
<div>
{!loading && (
<div>
<div>
Key "stringValue":{' '}
<input
type="text"
value={enteredValue}
onChange={e => setEnteredValue(e.target.value)}
onKeyPress={e => e.key === 'Enter' && setValue(enteredValue)}
/>
<button onClick={() => setValue(enteredValue)}>Set</button>
</div>
<div>
<strong>Stored value:</strong> {value}
</div>
</div>
)}
<div>
<button onClick={() => reset()}>Reset "stringValue" to default</button>
</div>
<hr />
{!loading2 && <div>Another "stringValue" hook (different hook, will synchronize): {value2}</div>}
</div>
);
}