Hone logo
Hone
Problems

Implementing a useSessionStorage Hook in React

Session storage provides a way to store data in the browser's session storage, persisting data only for the duration of the user's session (until the browser tab or window is closed). This challenge asks you to implement a custom React hook, useSessionStorage, that simplifies interacting with session storage, providing a React-friendly way to read, write, and update session data. This is useful for managing temporary state that needs to persist across re-renders within a single browser session.

Problem Description

You need to create a React hook called useSessionStorage that accepts a key as an argument and returns an array containing:

  1. The current value stored in session storage under the provided key.
  2. A function to update the value in session storage.

The hook should handle the following:

  • Initialization: When the component using the hook mounts, it should retrieve the value from session storage using the provided key. If the key doesn't exist in session storage, it should initialize the value to null.
  • Updating: The update function should accept a new value and store it in session storage under the provided key. The component should re-render after a successful update.
  • Type Safety: The hook should be written in TypeScript and provide type safety for the stored value.
  • Error Handling: While session storage errors are rare, the hook should gracefully handle potential errors during retrieval or storage. (For simplicity, you can log errors to the console instead of throwing exceptions).
  • Persistence: The stored value should persist across re-renders of the component using the hook.

Expected Behavior:

  • The hook should return an array where the first element is the current value (or null if not found) and the second element is an update function.
  • The update function should correctly store the new value in session storage.
  • The component should re-render whenever the value in session storage changes.

Examples

Example 1:

Input: useSessionStorage("myKey")
Output: [null, (newValue: string | null) => void]
Explanation: Initially, "myKey" doesn't exist in session storage, so the hook returns [null, updateFunction].

Example 2:

Input: useSessionStorage("myKey", "initialValue") // Initial value provided
Output: ["initialValue", (newValue: string | null) => void]
Explanation: "myKey" doesn't exist, so the hook initializes it with "initialValue" and returns [ "initialValue", updateFunction].

Example 3:

Input: useSessionStorage("myKey") after setting "myKey" to "hello" in session storage.
Output: ["hello", (newValue: string | null) => void]
Explanation: "myKey" exists in session storage with the value "hello", so the hook returns ["hello", updateFunction].

Constraints

  • The hook must be written in TypeScript.
  • The update function should accept a value of any type.
  • The hook should not throw errors; instead, log any errors to the console.
  • The hook should be compatible with React 18 or later.
  • The initial value parameter is optional. If not provided, the initial value should be null.

Notes

  • Consider using useState to manage the local state of the value.
  • Use sessionStorage.getItem() to retrieve the value from session storage.
  • Use sessionStorage.setItem() to store the value in session storage.
  • Use sessionStorage.removeItem() to remove the value from session storage (optional, but good practice).
  • Think about how to handle potential type mismatches between the stored value and the new value. While strict type checking isn't required, consider how to handle different data types gracefully.
  • The update function should be memoized to prevent unnecessary re-renders.
Loading editor...
typescript