pbeshai / use-query-params

React Hook for managing state in URL query parameters with easy serialization.

Home Page:https://pbeshai.github.io/use-query-params

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty string array value not parsed correctly

soulprovidr opened this issue · comments

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= []

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
}