Open KnutHelstad opened 9 months ago
After testing and perusing the documentation, I ended up with this code that worked as I wanted. Not sure if this is quite the right way.
import React, { useMemo } from 'react';
import { shallowEqual } from 'react-redux';
import { createSelector, lruMemoize, weakMapMemoize } from 'reselect';
import { useGetTagsQuery } from 'store/slices/lockalOPCUAAPI';
const useGetTagGroupOPC = (dbmame) => {
const selectTagsIndb = useMemo(() => {
const emptyArray = [];
return createSelector(
(result) => {
// console.log('result', result);
return result.data;
},
(_, db) => db,
(data, db) => data?.filter((tag) => tag.db === db) ?? [],
{
memoize: lruMemoize,
memoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 3
},
argsMemoize: lruMemoize,
argsMemoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 3
}
}
// (data, db) => exsampleArray ?? emptyArray
);
}, []);
const { opctags } = useGetTagsQuery(undefined, {
selectFromResult: (result) => {
return {
// ...result,
opctags: selectTagsIndb(result, dbmame)
};
}
});
return { opctags };
};
export default useGetTagGroupOPC;
Hello and thank you for a brilliant library. I'm using @reduxjs/toolkit 1.9.7 I'm struggling with two custom hooks with a useMemo createSelector. Both retrieve data from the same query. One only retrieves an object (which works) and the other retrieves an array (which doesn't work)
This is the hook that works
This is a hook that does not work and rerenders my component every time there is an update in
useGetTagsQuery
Example of data from
useGetTagsQuery
Need help here to understand what is wrong with
useGetTagGroupOPC
In advance, thank you