Not equal empty array reference with customParam
See original GitHub issueProblem:
Reproduce:
- 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(",");
}
};
useQueryParamslike this:
const config = {
filters: withDefault(commaArrayParams, ["hi"]),
sortBy: withDefault(StringParam, "a")
};
function App() {
const [query, setQuery] = useQueryParams(config);
...
}
- 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:
- is it an expected behaviour?
- when there is a non-value for
filtersin the url.query.filtersnot changes. (expected). - when change cached the returned empty array. so the return reference is the same.
query.filtersnot 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:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This is due to your decode function I believe.
withDefaultonly 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?In any case, I’d suggest using this approach for a comma delimited array encoding:
Published in v1.1.9