-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistentCounter.js
44 lines (40 loc) · 1.08 KB
/
PersistentCounter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import React, { useState, useEffect } from 'react';
function PostList() {
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
// Récupération des données depuis l’API
fetch('https://jsonplaceholder.typicode.com/posts')
.then((response) => {
if (!response.ok) {
throw new Error("Erreur lors du chargement des données");
}
return response.json();
})
.then((data) => {
setPosts(data);
setLoading(false);
})
.catch((err) => {
setError(err.message);
setLoading(false);
});
}, []);
if (loading) return <p>Chargement...</p>;
if (error) return <p>{error}</p>;
return (
<div>
<h2>Liste des articles</h2>
<ul>
{posts.slice(0, 10).map((post) => (
<li key={post.id}>
<h3>{post.title}</h3>
<p>{post.body}</p>
</li>
))}
</ul>
</div>
);
}
export default PostList;