React exp 004
React exp 004
Title: Create a React component that renders dynamic content fetched from an external data source,
such as a JSON API.
Code :
1.PaginatedUsers.jsx
import ‘./PaginatedUsers.css’;
const usersPerPage = 5;
setLoading(true);
setError("");
try {
setUsers(response.data.slice(startIndex, endIndex));
setLoading(false);
} catch (err) {
setLoading(false);
};
useEffect(() => {
fetchUsers(currentPage);
}, [currentPage]);
return (
<Container>
<UserList>
{users.map((user) => (
<UserItem key={user.id}>
<h3>{user.name}</h3>
<p>{user.email}</p>
</UserItem>
))}
</UserList>
<div>
<Button onClick={() => setCurrentPage((prev) => prev - 1)} disabled={currentPage === 1}>
Previous
</Button>
</div>
</Container>
);
};
function App() {
return (
<>
<PaginatedUsers/>
</>
Output: