mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-23 04:19:48 +00:00
Optimized code
This commit is contained in:
@@ -15,6 +15,7 @@ import { useFetch } from '../../hooks/useFetch';
|
|||||||
|
|
||||||
// Styled
|
// Styled
|
||||||
import Spinner from '../../commons/styles/Spinner';
|
import Spinner from '../../commons/styles/Spinner';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
type TUserModal = { user: TUser };
|
type TUserModal = { user: TUser };
|
||||||
|
|
||||||
@@ -54,15 +55,22 @@ const UserRow = ({ user }: TUserModal) => {
|
|||||||
|
|
||||||
const UserTable = () => {
|
const UserTable = () => {
|
||||||
const { data, isPending, errorMsg } = useFetch('users');
|
const { data, isPending, errorMsg } = useFetch('users');
|
||||||
let users: TUser[] = [];
|
const [users, setUsers] = useState<TUser[]>([]);
|
||||||
|
|
||||||
if (data) users = data;
|
useEffect(() => {
|
||||||
|
if (data) {
|
||||||
|
setUsers(data);
|
||||||
|
}
|
||||||
|
|
||||||
if (isPending) return <Spinner />;
|
if (errorMsg) {
|
||||||
|
console.error(errorMsg);
|
||||||
|
}
|
||||||
|
}, [data, errorMsg]);
|
||||||
|
|
||||||
if (errorMsg) console.error(errorMsg);
|
return (
|
||||||
|
<>
|
||||||
return users.length ? (
|
{isPending && <Spinner />}
|
||||||
|
{users.length ? (
|
||||||
<Direction>
|
<Direction>
|
||||||
<StyledOperationTable>
|
<StyledOperationTable>
|
||||||
<p>Sort / Search table</p>
|
<p>Sort / Search table</p>
|
||||||
@@ -86,6 +94,8 @@ const UserTable = () => {
|
|||||||
</Direction>
|
</Direction>
|
||||||
) : (
|
) : (
|
||||||
<Message>No data to show here!</Message>
|
<Message>No data to show here!</Message>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user