pbeshai / use-query-params

React Hook for managing state in URL query parameters with easy serialization.
https://pbeshai.github.io/use-query-params
ISC License
2.13k stars 95 forks source link

Empty string array value not parsed correctly #274

Open soulprovidr opened 11 months ago

soulprovidr commented 11 months ago

Hi – first of all, thanks for your work on this library.

I ran into what I believe is a bug today and would like to understand if there is any way to work around it.

Given the following param definition:

const [urlParams, setUrlParams] = useQueryParams({
    foo: ArrayParam,
  });

Expected behaviour:

URL params foo value
?foo=bar&foo= ['bar', '']
?foo= ['']

Actual behaviour:

URL params foo value
?foo=bar&foo= ['bar', '']
?foo= []
soulprovidr commented 11 months ago

It seems like the getEncodedValueArray function could be modified, although I might be blind to potential side effects:


function getEncodedValueArray(
  input: string | (string | null)[] | null | undefined
): (string | null)[] | null | undefined {
  if (input == null) {
    return input;
  }

  return input instanceof Array ? input : input === '' ? [''] : [input];  // Empty string no longer returns empty array
}