useAPI hook with demonstration on the login component

This commit is contained in:
Ben Mitchinson committed 2020-03-27 01:28:07 -05:00
1 parent 261864f903
commit e8b83dbcb9
3 files changed
+34

No files matched your search

+7
View File
@@ -1,8 +1,11 @@
import React, { ReactElement } from 'react'
import useApi from '../hooks/useApi'
import {InputItem, Button} from 'antd-mobile'
import '../scss/Login.scss'
export default function Login(): ReactElement {
const {response, error, isLoading} = useApi("")
return (
<>
<div id="loginWrapper">
@@ -19,6 +22,10 @@ export default function Login(): ReactElement {
</div>
</div>
</div>
<p>Loading API Fetch: {isLoading.toString()}</p>
{response &&
<p>API Fetch Result: {JSON.stringify(response)}</p>
}
</>
)
}
+26
View File
@@ -0,0 +1,26 @@
import React from 'react'
const useFetch = (route: string) => {
const [response, setResponse] = React.useState(null);
const [error, setError] = React.useState(null);
const [isLoading, setIsLoading] = React.useState(false);
React.useEffect(() => {
const fetchData = async () => {
setIsLoading(true);
try {
const res = await fetch("http://localhost:4000/" + route);
const json = await res.json();
setResponse(json);
setIsLoading(false)
} catch (error) {
setError(error);
}
};
fetchData();
}, []);
return { response, error, isLoading };
};
export default useFetch
+1
View File
@@ -6,6 +6,7 @@
"dom.iterable",
"esnext"
],
"strictNullChecks": false,
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,