question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not equal empty array reference with customParam

See original GitHub issue

Problem:

Reproduce:

  1. have two query params, one is custom ArrayParam, another one is StringParam. The custom param provider looks like this:
const commaArrayParams = {
  encode: (arr) => {
    if (arr.length === 0) {
      return undefined;
    }
    return arr.join(",");
  },
  decode: (str) => {
    if (!str || Array.isArray(str)) {
      return [];
    }

    return str.split(",");
  }
};
  1. useQueryParams like this:
const config = {
  filters: withDefault(commaArrayParams, ["hi"]),
  sortBy: withDefault(StringParam, "a")
};

function App() {
  const [query, setQuery] = useQueryParams(config);
  ...
}
  1. Toggle the sortBy query params by calling setQuery({ sortBy: query.sortBy === "a" ? "b" : "a"}).

Expect

query.filters should not change.

Actual

query.filters changed every time.

Sandbox to reproduce: https://codesandbox.io/s/admiring-dirac-fhv7o

Some findings & Questions:

  1. is it an expected behaviour?
  2. when there is a non-value for filters in the url. query.filters not changes. (expected).
  3. when change cached the returned empty array. so the return reference is the same. query.filters not changes.
const emptyArr = [];
const commaArrayParams = {
  encode: (arr) => {
    if (arr.length === 0) {
      return undefined;
    }
    return arr.join(",");
  },
  decode: (str) => {
    if (!str || Array.isArray(str)) {
      return emptyArr;
    }

    return str.split(",");
  }
};

Why this happened? When is the decoded function invoked? Thanks for help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pbeshaicommented, Nov 4, 2020

This is due to your decode function I believe. withDefault only kicks in if a nully response is returned from decode. Does it work as you expect if you switch your commaArrayParams to be the following?

const commaArrayParams = {
  encode: (arr) => {
    if (arr.length === 0) {
      return undefined;
    }
    return arr.join(",");
  },
  decode: (str) => {
    if (!str || Array.isArray(str)) {
      return undefined; // <-- changed this
    }

    return str.split(",");
  }
};

In any case, I’d suggest using this approach for a comma delimited array encoding:

import {
  encodeDelimitedArray,
  decodeDelimitedArray
} from "use-query-params";

/** Uses a comma to delimit entries. e.g. ['a', 'b'] => qp?=a,b */
const CommaArrayParam = {
  encode: (array) => encodeDelimitedArray(array, ','),
  decode: (arrayStr) => decodeDelimitedArray(arrayStr, ',')
};


const config = {
  filters: withDefault(CommaArrayParam, ["hello"]),
  sortBy: withDefault(StringParam, "asc")
};
0reactions
pbeshaicommented, Nov 8, 2020

Published in v1.1.9

Read more comments on GitHub >

github_iconTop Results From Across the Web

Empty array does not equal itself in Javascript? - Stack Overflow
Conclusion: we compare object instances via the memory pointer/address or we can say references, and primitive types via the real value ...
Read more >
Array | Apple Developer Documentation
Use the isEmpty property to check quickly whether an array has any elements, or use the count property to find the number of...
Read more >
Firebase.Firestore.Query Class Reference - Google
The fieldValues array must not be null or empty (though elements of the array may be), or have more values than query has...
Read more >
TypeError: Reduce of empty array with no initial value
This error is raised when an empty array is provided because no initial value can be returned in that case.
Read more >
Full Text Bug Listing - Red Hat Bugzilla
Check detail of that host, it should print the parameters > hammer host info ... However in the non --parameters flag it's now...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found