-
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { useEffect, useState } from "react"; | ||
| function UseToggle() { | ||
| const [Infor, setInfor] = useState([]); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inside the 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; | ||
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
uselower-cased.Additionally, when creating a new hook please make sure you name it carefully.
useToggleis not really a fitting name in this case, because we are not toggling anything.Instead name it into something like
useFetchDataoruseFetch.