paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
478 stars 13 forks source link

hiding default value with arrays and objects #72

Open brennerm opened 5 months ago

brennerm commented 5 months ago

Describe the bug

Not sure if this is a bug, but let me explain.

When I create a new query param store with an empty array as the default value and showDefaults set to false, everything is fine on the first load. The query param is not added to the URL.

Adding an element to the array and the URL updates accordingly.

But now when I remove the object again or assign [] to the store, the query param is added to the URL with an empty array value.

@paoloricciuti Would love to hear you opinion on what the expected behavior should be. IMO the query param should disappear again after it has been reset to its default value.

Already have an idea that the issue comes from comparison by reference instead of comparison by value, but want to wait for your feedback first, before jumping into any code.

Cheers!

Reproduction

// creating the store, no query param in the URL
const params = queryParam('params', ssp.array([]), { showDefaults: false });

// adding element to store array, URL updates to ?params=["foo"]
$params = ['foo']

// resetting store to empty array, URL updates to ?params=[]
$params = []

Logs

No response

paoloricciuti commented 5 months ago

Oh yeah I'm pretty sure is for reference equality...I will check as soon as I can, thanks for reporting