Skip to content

Conversation

@Mustafa88A
Copy link

No description provided.

@@ -0,0 +1,26 @@
import { useEffect, useState } from "react";
function UseToggle() {
Copy link
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.

@@ -0,0 +1,26 @@
import { useEffect, useState } from "react";
function UseToggle() {
const [Infor, setInfor] = useState([]);
Copy link
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 data = await response.json();
setInfor(data);
setIsLoading(false);
} else console.log("error");
Copy link
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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants