Built Twitter Like App - 11

Install SWR & axios

1
2
npm install swr
npm install axios

Add Hooks for CurrentUser

Add file libs/fetcher.ts

1
2
3
4
5
import axios from "axios";

const fetcher = (url: string) => axios.get(url).then( (res) => res.data);

export default fetcher;

Add file hooks/useCurrentUser.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import useSWR from 'swr';

import fetcher from '../libs/fetcher';

const useCurrentUser = () => {
const { data, error, isLoading, mutate } = useSWR('/api/current', fetcher)

return {
data,
error,
isLoading,
mutate
}
};

export default useCurrentUser;

请我喝杯咖啡吧~

支付宝
微信