-
Hello, To make this more concrete, it looks roughly like this. Trivial custom hook that wraps useQueries: function useDatasets(datasetIds: string[]) {
const queries = useMemo(() => {
return datasetIds.map((id) => ({
queryKey: ["dataset", id],
queryFn: fetchDataset
}));
}, [datasetIds]);
return useQueries(queries);
} Component that uses custom hook and wants to run some calculation when the data changes: function SomeComponent(props): JSX.Element {
const { datasetIds } = props;
const [someState, setSomeState] = useState();
const datasets = useDatasets(datasetIds);
// This effect runs every time SomeComponent renders even though the loaded data is unchanged
useEffect(() => {
const isSuccess = datasets.every((result) => result.isSuccess);
const isLoading = datasets.some((result) => result.isLoading);
if (isSuccess && !isLoading) {
const data = datasets.filter((result) => result.data != null).map((result) => result.data);
// perform calculation and set state
const newSomeState = calcSomeState(data);
setSomeState(newSomeState);
}
}, [datasets, setSomeState]);
} Am I looking at this problem the wrong way? Is there a way to get the desired behavior that does not depend on useQueries returning arrays with the same object identity on each call? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
yes, it does. There's also no need to add apart from that, data transformation shouldn't need So, long story short: There is no good, built-in way to aggregate data over a dynamic array from |
Beta Was this translation helpful? Give feedback.
yes, it does. There's also no need to add
useMemo
to thequeries
input, that doesn't change that.apart from that, data transformation shouldn't need
useEffect
and state. What you'd probably want is anotheruseMemo
instead of theuseEffect
. But this would suffer from the same problem:useMemo
would also just run on every render.So, long story short: There is no good, built-in way to aggregate data over a dynamic array from
useQueries
. If it helps, eachdata
object inside your array will be kept referentially stable unless something changes insi…