Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd8f33b6

Browse files
committed
solution: useEffect to load messages
1 parent8b9c28c commitd8f33b6

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

‎src/components/Feed/index.js‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import "./styles.css";
66
importfetchDatafrom"../../services/api";
77

88
exportconstFeed=()=>{
9-
constloading=true;
10-
consterror=null;
11-
constdata=null;
9+
const[loading,setLoading]=React.useState(true);
10+
const[error,setError]=React.useState(null);
11+
const[data,setData]=React.useState(null);
1212

13-
// TODO: useEffect to load state from fetchData
14-
fetchData("messages")
15-
.then(messages=>{
16-
data=messages;
17-
loading=false;
18-
})
19-
.catch(error=>{
20-
error=error;
21-
});
13+
React.useEffect(()=>{
14+
fetchData("messages")
15+
.then(result=>{
16+
setData(result);
17+
setLoading(false);
18+
})
19+
.catch(error=>{
20+
setError(error);
21+
});
22+
},[]);
2223

2324
if(loading){
2425
return(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp