-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserForm.jsx
More file actions
33 lines (28 loc) · 985 Bytes
/
UserForm.jsx
File metadata and controls
33 lines (28 loc) · 985 Bytes
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
import { useEffect, useState } from 'react'
import { fetchUsers } from '../api'
export default function UserList({ refreshKey }) {
const [users, setUsers] = useState([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState('')
useEffect(() => {
let isMounted = true
setLoading(true)
fetchUsers()
.then(data => { if (isMounted) setUsers(data) })
.catch(err => { if (isMounted) setError(err.message) })
.finally(() => { if (isMounted) setLoading(false) })
return () => { isMounted = false }
}, [refreshKey])
if (loading) return <p>Loading users…</p>
if (error) return <p style={{color:'crimson'}}>Error: {error}</p>
if (!users.length) return <p>No users yet. Add one!</p>
return (
<ul style={{lineHeight: '1.9'}}>
{users.map(u => (
<li key={u._id}>
<strong>{u.name}</strong> — <code>{u.email}</code>
</li>
))}
</ul>
)
}