-
Notifications
You must be signed in to change notification settings - Fork 11
useEffect with style v1 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| @@ -0,0 +1,26 @@ | |||
| import { useEffect, useState } from "react"; | |||
| function UseToggle() { | |||
There was a problem hiding this comment.
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([]); | |||
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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);
No description provided.