使用 useQueries 时抛出数组错误

问题描述 投票:0回答:0

我正在尝试使用 TanStack 挂钩中的 UseQueries 挂钩。

我的代码是这样的:

import React from 'react';
import { useParams } from 'react-router-dom';
import { useQueries } from 'react-query';
import { BaseUrl } from "../common/Web";
import axios from 'axios';

const EditBug = () => {
    const { id } = useParams();
    const [projectsQuery, bugsQuery]
        = useQueries({
            queries: [
                {
                    queryKey: ['projectsList', 'hi'],
                    queryFn: ({ queryKey }) => {
                        axios.get(`${BaseUrl}/projects/listall`)
                            .then(response => response.data);
                    }
                },
                {
                    queryKey: ['bugToEdit', id],
                    queryFn: ({ queryKey }) => {
                        axios.get(`${BaseUrl}/Bug/GetBug/${queryKey[1]}`)
                            .then(response => response.data);
                    }
                }
            ],
        });
        
// etc.

// ...      

我在控制台中收到以下错误,但只有当代码中的那个点第二次被击中时:

useQueries.js:13 Uncaught TypeError: queries.map is not a function

这是被扔进 Tanstack 钩子里的。代码如下所示:

var defaultedQueries = useMemo(function () {
return queries.map(function (options) {
  var defaultedOptions = queryClient.defaultQueryObserverOptions(options); // Make sure the results are already in fetching state before subscribing or updating options

  defaultedOptions.optimisticResults = true;
  return defaultedOptions;
});
}, [queries, queryClient]);

第一次打第2行,没问题。第二次,查询似乎未定义。而且我不明白我做错了什么。

任何帮助将不胜感激。 谢谢

reactjs tanstackreact-query
© www.soinside.com 2019 - 2024. All rights reserved.