細かい書き方は正しくないかもしれませんが、一旦getできたのでメモとして。
Some
というコンポーネントを作成し、その中でaxios.get()
を使っています。
Some.tsx
import React from 'react';
import axios from 'axios';
interface SomeResponse {
id: number;
title: string;
...
}
export default class Some extends React.Component {
clicked() {
axios.get<SomeResponse[]>('https://hogehoge.com/api/fugafuga')
.then(res => {console.table(res.data)})
.catch(error => {console.log(error)})
}
render() {
return (
<button onClick={this.clicked}>click</button>
);
}
}
取得したデータの型をSomeResponse
という名前でinterface
として定義しておき、
axios.get
の後ろにジェネリックとして渡します。
axios.get()
の返り値はPromise
型なので、後ろに.then()
や.catch()
を付けられます。
http通信のレスポンスにはdata
プロパティが含まれているので、
それをconsole.table()
に渡すことでデベロッパーツールのコンソールに表形式で出力します。