Skip to content

Way of deleting value from localStorage (not just resetting) #308

@MarcusCaspeco

Description

@MarcusCaspeco

Hey, thanks for the great library.

There's one thing I couldn't figure out how to do:

If a store's value is set to undefined or null, I want to delete it from localstorage (localStorage.removeItem(key)), which is different from setting the value to undefined or null.

I was originally trying to accomplish this with the beforeWrite function, but since that doesn't allow me to stop the write to localStorage, it doesn't work.

My proposal is a .delete()-function that would behave just like the .reset()-function except it resets the value to default AND removes the item from localstorage. Alternatively it could be called something like .hardReset() or even be a parameter in the .reset()-function. As a consumer, I would use this function instead of setting the value to undefined if I actually want to remove it from localStorage as well.

Alternatively, let me cancel beforeWrite, as was discussed in #250 (comment). This would be a bit nicer for me as a consumer, as I could handle this logic generally and not have to worry about the difference of setting a value to undefined vs invoking .delete(), but I imagine this is more error-prone and complex to implement.

Or is there a way to accomplish this already?

Related issue: #217

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions