ReactJS
1import React from "react";2import axios from "axios";3import {tokenContext} from "../shared/context/tokenContext";4
5interface IUserData {6name?: string;7iconImg?: string;8}
9
10export function useUserData(){11const [data, setData] = React.useState<IUserData>({});12const token = React.useContext(tokenContext);13React.useEffect(() => {14if(token == 'undefined' || token == ''){15return;16}17axios.get('https://oauth.reddit.com/api/v1/me', {18headers: {Authorization: `Bearer ${token}`}19}20).then((resp) => {21const userData = resp.data;22setData({name: userData.name, iconImg: userData.icon_img});23})24.catch(console.log)25}, [token]);26
27return [data];28}