In my database, I have multiple exercises and I display all of them in the main page. I want to be able to click the "View Exercise" button to see information about that exercise itself.
App.js route code:
const App = () => {
return (
<main>
<Switch>
<Route path="/" component={MainPage} exact />
<Route path="/exercises/:name" component={Exercise} exact/>
<Route component={Error} />
</Switch>
</main>
);
};
Code component that should show a specific exercise:
import React from "react";
function Exercise(props) {
return (
<div>
<h1>hi {props.name} !</h1>
</div>
);
}
export default Exercise;
How I display the list of exercises:
function Exercises() {
const [exercise, setExercise] = useState([]);
useEffect(() => {
const getAPI = async () => {
const response = await fetch('http://localhost:8080/');
const data = await response.json();
try {
console.log(data);
setExercise(data);
} catch (error) {
console.log(error);
}
};
getAPI();
}, []);
return (
<div>
<h2 id="exercises">List of Exercises</h2>
<Row>
{exercise.map((data) => (
<ExerciseCard
key = {data._id}
name = {data.name}
img = {data.image}
/>
))}
</Row>
</div>
);
}
export default Exercises;
The button link:
<Link to= {{
pathname: `/exercises/${props.name}`
}}
>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…