【发布时间】:2021-11-11 01:11:08
【问题描述】:
我无法正确形成类型的参数。
这是我的界面
export interface IAssetGetManyResponseDto<T> {
items: T[];
totalCount: number;
}
在我的应用程序中,我想将参数传递给 useState 挂钩,并使用 useState 和 useEffect 获取一些具有IAssetGetManyResponseDto 类型响应的数据。
这是我如何尝试将类型传递给参数以及我正在尝试做什么的更大背景
import { useState, useEffect } from 'react';
import { getAllAssets } from '../api/api';
import { NewAssetTable } from "../components/NewAssetTable"
import { IAssetGetManyResponseDto } from '../types/types';
export const AssetManagementTable = () => {
const [data, setData] = useState<IAssetGetManyResponseDto>()
const getAssets = async () => {
const assets = await getAllAssets();
console.log(data)
console.log(assets)
setData(assets)
}
useEffect(() => {
getAssets()
}, []);
return (
<div>
<NewAssetTable items={data.items} />
</div>
);
};
错误:
Generic type 'IAssetGetManyResponseDto<T>' requires 1 type argument(s). TS2314
8 | export const AssetManagementTable = () => {
9 |
> 10 | const [data, setData] = useState<IAssetGetManyResponseDto>()
| ^
11 |
12 | const getAssets = async () => {
13 | const assets = await getAllAssets();
论证应该是什么样的?一旦我正确地制定了响应,我将如何访问它?
冒着添加不必要的代码并被遗忘的风险,我会到此结束!
谢谢!
【问题讨论】:
标签: reactjs typescript typescript-generics use-state