What's the correct way to return an empty array as fallback from a selector? #741
-
I have a selector which returns an empty array as fallback if the main selector function returns a nullish result. I started off with a seemingly simple selector like this: const getResult = (state) => state.result ?? []; The linter correctly complains that this selector lacks memoization, because
I then tried using reselect to memoize the selector like this: const getResult = createSelector((state) => state.result, result => result ?? []); The linter also complains about this, since when the fallback isn't triggered, the combiner simply returns the input:
I know I can turn off the One solution I came up with is to "cash" the empty array manually, e.g.: const emptyArray = [];
const getResult = (state) => state.result ?? emptyArray; Technically, that works but it kinda feels wrong. Suggestions would be appreciated. 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Actually what you have is sufficient: const emptyArray = [];
const getResult = (state) => state.result ?? emptyArray; |
Beta Was this translation helpful? Give feedback.
We do: https://reselect-docs.netlify.app/usage/handling-empty-array-results