I'm trying to use react router in the following code, and render the CourseDetail component on button click. However, nothing renders (just goes to the appropriate URL).
I don't want to put it in my App.js file with all my other routes, as I want to use the props in this specific parent component. Any help is appreciated!
Also edit: ${match.url} gives /student/courses
import {
BrowserRouter,
Route,
Switch,
withRouter,
Link,
} from "react-router-dom";
import { Card, Button } from "react-bootstrap";
import "./style.css";
import CourseDetail from "./CourseDetail";
class CourseItem extends React.Component {
render() {
const { course, match } = this.props;
return (
<div>
<Card style={{ width: "18rem" }}>
<Card.Body>
<Card.Title>
{course.subject} {course.code}: {course.name}
</Card.Title>
<Button variant="primary">
<Link to={`${match.url}/${course.subject}${course.code}`}>
Access Course
</Link>
</Button>
</Card.Body>
</Card>
<BrowserRouter>
<Switch>
<Route
exact
path="/student/courses/:course"
render={(props) => <CourseDetail {...props} course={course} />}
/>
</Switch>
</BrowserRouter>
</div>
);
}
}
export default withRouter(CourseItem);
Course Item is rendered by Course List:
import React from "react";
import { withRouter } from "react-router-dom";
import Header from "../../../components/Header";
import CourseItem from "./CourseItem";
import { CardDeck, Container } from "react-bootstrap";
import axios from "axios";
class CourseList extends React.Component {
constructor(props) {
super(props);
this.state = {
courses: [],
isLoading: false,
error: "",
};
}
render() {
const { courses } = this.state;
return (
<div className="course-list">
<Header title="Courses & Labs" />
<Container>
<CardDeck>
{courses &&
courses.map((course) => {
return <CourseItem course={course} />;
})}
</CardDeck>
</Container>
</div>
);
}
}
export default withRouter(CourseList);
App renders CourseList:
import React from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import data from "./data.json";
import Navigation from "./components/Navigation";
import Footer from "./components/Footer";
import Home from "./pages/Home";
import About from "./pages/About";
import Research from "./pages/Research";
import Publications from "./pages/Publications";
import HallOfFame from "./pages/HallOfFame";
import EvaluationList from "./pages/Student/TAEvaluations/EvaluationList";
import CourseList from "./pages/Student/Courses/CourseList";
import CourseDetail from "./pages/Student/Courses/CourseDetail";
import CourseEditorList from "./pages/Prof/CourseEditor/CourseEditorList";
import CourseEditorDetail from "./pages/Prof/CourseEditor/CourseEditorDetail";
import EvaluationEditorList from "./pages/Prof/EvaluationEditor/EvaluationEditorList";
import EvaluationEditorDetail from "./pages/Prof/EvaluationEditor/EvaluationEditorDetail";
import ReportList from "./pages/Prof/ReportEditor/ReportList";
import ReportDetail from "./pages/Prof/ReportEditor/ReportDetail";
class App extends React.Component {
render() {
const mockCourses = data.Courses;
return (
<div className="App">
<BrowserRouter>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/research" component={Research} />
<Route path="/publications" component={Publications} />
<Route path="/halloffame" component={HallOfFame} />
<Route exact path="/student/courses" component={CourseList} />
</Switch>
</BrowserRouter>
<Footer />
</div>
);
}
}
export default App;