useSWR
This commit is contained in:
26
src/App.tsx
26
src/App.tsx
@ -1,5 +1,4 @@
|
||||
import React from 'react';
|
||||
import {useContext} from 'react'
|
||||
import './App.css';
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom';
|
||||
import Login from './components/Login';
|
||||
@ -15,11 +14,32 @@ import FileUpload from './components/FileUpload';
|
||||
import Profil from './components/Profil';
|
||||
import useSWR from 'swr';
|
||||
import {getUser} from './services/PhpApi'
|
||||
import toast, { Toaster } from 'react-hot-toast';
|
||||
import { TUser } from './context/UserContext';
|
||||
|
||||
const App: React.FC = () =>
|
||||
{
|
||||
const {data} = useSWR("user", getUser);
|
||||
const user = data.user;
|
||||
const {data, error, isLoading} = useSWR("/user", getUser);
|
||||
if (error)
|
||||
{
|
||||
toast.error(error.message);
|
||||
return (
|
||||
<>
|
||||
<Toaster toastOptions={{ position: "top-center" }} />
|
||||
<div>failed to load</div>
|
||||
</>);
|
||||
}
|
||||
if (isLoading) return (<div>loading...</div>);
|
||||
|
||||
let user: TUser | null;
|
||||
if(data.success)
|
||||
{
|
||||
user = data.data;
|
||||
}
|
||||
else
|
||||
{
|
||||
user = null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
|
||||
Reference in New Issue
Block a user