forked from 4GeeksAcademy/react-hello-webapp-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 972
Expand file tree
/
Copy pathSingle.jsx
More file actions
37 lines (32 loc) · 1.66 KB
/
Single.jsx
File metadata and controls
37 lines (32 loc) · 1.66 KB
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
// Import necessary hooks and components from react-router-dom and other libraries.
import { Link, useParams } from "react-router-dom"; // To use link for navigation and useParams to get URL parameters
import PropTypes from "prop-types"; // To define prop types for this component
import rigoImageUrl from "../assets/img/rigo-baby.jpg" // Import an image asset
import useGlobalReducer from "../hooks/useGlobalReducer"; // Import a custom hook for accessing the global state
// Define and export the Single component which displays individual item details.
export const Single = props => {
// Access the global state using the custom hook.
const { store } = useGlobalReducer()
// Retrieve the 'theId' URL parameter using useParams hook.
const { theId } = useParams()
const singleTodo = store.todos.find(todo => todo.id === parseInt(theId));
return (
<div className="container text-center">
{/* Display the title of the todo element dynamically retrieved from the store using theId. */}
<h1 className="display-4">Todo: {singleTodo?.title}</h1>
<hr className="my-4" /> {/* A horizontal rule for visual separation. */}
{/* A Link component acts as an anchor tag but is used for client-side routing to prevent page reloads. */}
<Link to="/">
<span className="btn btn-primary btn-lg" href="#" role="button">
Back home
</span>
</Link>
</div>
);
};
// Use PropTypes to validate the props passed to this component, ensuring reliable behavior.
Single.propTypes = {
// Although 'match' prop is defined here, it is not used in the component.
// Consider removing or using it as needed.
match: PropTypes.object
};