Skip to content

Commit 756a410

Browse files
committed
move function inside effect
1 parent e7c4cfa commit 756a410

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/useDataApiHook-example/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ const useDataApi = (initialUrl, initialData) => {
77
const [isLoading, setIsLoading] = useState(false);
88
const [isError, setIsError] = useState(false);
99

10-
const fetchData = async () => {
11-
setIsError(false);
12-
setIsLoading(true);
10+
useEffect(() => {
11+
const fetchData = async () => {
12+
setIsError(false);
13+
setIsLoading(true);
1314

14-
try {
15-
const result = await axios(url);
15+
try {
16+
const result = await axios(url);
1617

17-
setData(result.data);
18-
} catch (error) {
19-
setIsError(true);
20-
}
18+
setData(result.data);
19+
} catch (error) {
20+
setIsError(true);
21+
}
2122

22-
setIsLoading(false);
23-
};
23+
setIsLoading(false);
24+
};
2425

25-
useEffect(() => {
2626
fetchData();
2727
}, [url]);
2828

0 commit comments

Comments
 (0)