在打字稿中将接口设置为API的响应

时间:2020-07-09 07:22:02

标签: javascript reactjs typescript asynchronous axios

我正在使用打字稿和React开发网页。

调用API时,我是否声明所有响应的interface

这是我的React代码。


// Do I have to do this all times ?
interface DataObj {
  bred_for: string;
  breed_group: string;
  height: { [key: string]: string };
  id: number;
  life_span: string;
  name: string;
  origin: string;
  temperament: string;
  weight: { [key: string]: string };
}

const Main: React.FC<{}> = () => {
  const [data, setData] = useState<DataObj[] | null>(null);

  useEffect(() => {
    const fetchBreeds = async () => {
      const data: AxiosResponse = await apis.getBreeds();
      setData(data.data);
    };
    fetchBreeds();
  }, []);
  return (
  ...

我不想写整个回复的key。救救我!

0 个答案:

没有答案