Skip to content

useSessionStorage and useLocalStorage hooks fall back to initialValue on removal instead of null #344

Open
@carlos-aize

Description

@carlos-aize

When you call the setter with null (or undefined), the hook correctly removes the item from storage—but because of the fallback logic, it then returns the original initialValue instead of null (or undefined).

Steps to reproduce:

Install @uidotdev/usehooks

In your component:

const [value, setValue] = useLocalStorage<string>('foo', 'default');
setValue(null);

Observe that localStorage.getItem('foo') is removed, yet value === 'default'

Expected behavior:

After removal, the hook’s state should be null (or undefined), regardless of what initialValue was.

Actual behavior:

The hook returns initialValue whenever the stored item is missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions