Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions src/components/Item.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
import { useEffect, useState } from "react";
import UseToggle from "../hooks/UseToggle";

function Item() {
const [Infor, setInfor] = useState([]);
const [isLoading, setIsLoading] = useState(false);

const fetchData = async () => {
try {
setIsLoading(true);
const response = await fetch(
"https://jsonplaceholder.typicode.com/users"
);
if (response.ok) {
const data = await response.json();
setInfor(data);
setIsLoading(false);
} else console.log("error");
} catch (error) {
console.log("error massage", error);
}
};
useEffect(() => {
fetchData();
}, []);
const [fetchData, Infor, isLoading] = UseToggle();
return (
<div className="m-20 flex justify-around items-center flex-wrap gap-12">
{isLoading ? (
Expand All @@ -30,10 +11,11 @@ function Item() {
Infor?.map((info) => {
return (
<div
className="w-[380px] h-44 border-solid border-2 border-white rounded-3xl size text-xl flex flex-col items-center shadow-cards
key={info.id}
className="w-[380px] h-44 border-solid border-2 border-black rounded-3xl size text-xl flex flex-col items-center shadow-cards
transition ease-in-out delay-150 bg-one hover:-translate-y-1 hover:scale-110 hover: duration-300 "
>
<h2 className="bg-two m-5 w-full text-center text-3xl ">
<h2 className=" m-5 w-full text-center text-3xl font-bold ">
{info.name}
</h2>
<p>{info.email}</p>
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/UseToggle.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState } from "react";
function UseToggle() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you create a react hook, you have to name it correctly, with the use lower-cased.

Additionally, when creating a new hook please make sure you name it carefully.
useToggle is not really a fitting name in this case, because we are not toggling anything.
Instead name it into something like useFetchData or useFetch.

const [Infor, setInfor] = useState([]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you create a new state, make sure you follow the naming conventions.
the value and setter should be in camel case:

const [infor, setInfor] = useState([]);

const [isLoading, setIsLoading] = useState(false);

const fetchData = async () => {
try {
setIsLoading(true);
const response = await fetch(
"https://jsonplaceholder.typicode.com/users"
);
if (response.ok) {
const data = await response.json();
setInfor(data);
setIsLoading(false);
} else console.log("error");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside the else clause, you should set isLoading to false, not doing so would make isLoading always true in case an error happens.

if (response.ok) {
        const data = await response.json();
        setInfor(data);
        setIsLoading(false);
      } else {
        console.log("error");
        setIsLoading(false);
      }

or

if (response.ok) {
        const data = await response.json();
        setInfor(data);
      } else {
        console.log("error");
     }
    setIsLoading(false);

} catch (error) {
console.log("error massage", error);
}
};
useEffect(() => {
fetchData();
}, []);
return [fetchData, Infor, isLoading];
}
export default UseToggle;