即使get在起作用,也没有显示数据
我目前正在尝试获取在我的React应用程序上显示的数据。
我的查询设置为:
interface ResultWrapper<T> {
result: T;
}
export interface FinancialData {
handleViewData: () => void;
symbol: string;
name: string;
stockExchange: string;
exchangeShortName: string;
currency: string;
}
export const useGetFinancialDataKey = 'get-financial-data';
const useGetFinancialData = (
options: UseQueryOptions<FinancialData[], string> = {}
): UseQueryResult<FinancialData[], string> =>
useQuery<FinancialData[], string>(
useGetFinancialDataKey,
async () =>
(await axiosInstance.get<ResultWrapper<FinancialData[]>>.
('https://financialmodelingprep.com/api/v3/search?
query=AA&limit=10&exchange=NASDAQ&apikey=ENTERAPIKEY'
)).data.result, options
);
export default useGetFinancialData;
但是,每当我转到主页时,什么也没有显示,每当我在finance.tsx文件中调用查询时,我
const {data: financialData} = useGetFinancialData();
在finance.tsx中使用完整文件时就会得到一个不确定的内容。tsx看起来像这样
const Finance: React.FC = () => {
const {data: financialData} = useGetFinancialData();
const [searched, setSearched] = useState<string>('');
const [dataToView, setDataToView] = useState<FinancialData>();
const [isViewDialogOpen, setIsViewDialogOpen] = useState(false);
const [page, setPage] = useState(0);
const PER_PAGE = 10;
const handleViewData = useCallback(
(data: FinancialData) => {
setDataToView(data)
setIsViewDialogOpen(true);
},
[setDataToView, setIsViewDialogOpen]
);
const filteredRows = useMemo(()=>{
const query = searched.toLowerCase();
setPage(0);
return financialData?.filter(
(item) =>
item.name.toLowerCase().includes(query)
);
}, [searched, financialData]);
return (
<Paper elevation={4}>
<TableContainer>
<Toolbar>
<SearchBar
placeholder="Search By Name"
value={searched}
onChange={(searchVal: string) => setSearched(searchVal)}
onCancelSearch={() => setSearched('')}
/>
</Toolbar>
<Table>
<TableHead>
<TableRow>
<TableCell>Symbol</TableCell>
<TableCell>Name</TableCell>
<TableCell>Currency</TableCell>
<TableCell>Stock Exchange</TableCell>
<TableCell>Exchange Short Name</TableCell>
</TableRow>
</TableHead>
<TableBody>
{filteredRows &&
filteredRows.slice(page * PER_PAGE, page * PER_PAGE + PER_PAGE)
.map((row,i) => (
<FinancialDataRow
key = {row.name}
handleViewData={() => handleViewData(row)}
symbol={row.symbol}
name={row.name}
currency={row.currency}
stockExchange={row.stockExchange}
exchangeShortName={row.exchangeShortName}
/>
))}
</TableBody>
</Table>
</TableContainer>
</Paper>
);
};
export default Finance;
I am currently trying to get data to display on my React App.
My query is set to:
interface ResultWrapper<T> {
result: T;
}
export interface FinancialData {
handleViewData: () => void;
symbol: string;
name: string;
stockExchange: string;
exchangeShortName: string;
currency: string;
}
export const useGetFinancialDataKey = 'get-financial-data';
const useGetFinancialData = (
options: UseQueryOptions<FinancialData[], string> = {}
): UseQueryResult<FinancialData[], string> =>
useQuery<FinancialData[], string>(
useGetFinancialDataKey,
async () =>
(await axiosInstance.get<ResultWrapper<FinancialData[]>>.
('https://financialmodelingprep.com/api/v3/search?
query=AA&limit=10&exchange=NASDAQ&apikey=ENTERAPIKEY'
)).data.result, options
);
export default useGetFinancialData;
I have checked the network tab, and I am getting a response back:
However, whenever I go to the home page, nothing displays and whenever I call the query in my finance.tsx file, I get an undefined when using
const {data: financialData} = useGetFinancialData();
The full file in finance.tsx looks something like this:
const Finance: React.FC = () => {
const {data: financialData} = useGetFinancialData();
const [searched, setSearched] = useState<string>('');
const [dataToView, setDataToView] = useState<FinancialData>();
const [isViewDialogOpen, setIsViewDialogOpen] = useState(false);
const [page, setPage] = useState(0);
const PER_PAGE = 10;
const handleViewData = useCallback(
(data: FinancialData) => {
setDataToView(data)
setIsViewDialogOpen(true);
},
[setDataToView, setIsViewDialogOpen]
);
const filteredRows = useMemo(()=>{
const query = searched.toLowerCase();
setPage(0);
return financialData?.filter(
(item) =>
item.name.toLowerCase().includes(query)
);
}, [searched, financialData]);
return (
<Paper elevation={4}>
<TableContainer>
<Toolbar>
<SearchBar
placeholder="Search By Name"
value={searched}
onChange={(searchVal: string) => setSearched(searchVal)}
onCancelSearch={() => setSearched('')}
/>
</Toolbar>
<Table>
<TableHead>
<TableRow>
<TableCell>Symbol</TableCell>
<TableCell>Name</TableCell>
<TableCell>Currency</TableCell>
<TableCell>Stock Exchange</TableCell>
<TableCell>Exchange Short Name</TableCell>
</TableRow>
</TableHead>
<TableBody>
{filteredRows &&
filteredRows.slice(page * PER_PAGE, page * PER_PAGE + PER_PAGE)
.map((row,i) => (
<FinancialDataRow
key = {row.name}
handleViewData={() => handleViewData(row)}
symbol={row.symbol}
name={row.name}
currency={row.currency}
stockExchange={row.stockExchange}
exchangeShortName={row.exchangeShortName}
/>
))}
</TableBody>
</Table>
</TableContainer>
</Paper>
);
};
export default Finance;
As you can see from below, nothing is returned because it is being returned as undefined, even though the data is being fetched:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢@windowsill的帮助。
thank you @windowsill for helping.