I am trying to perform dynamic page routing with react router, and thus far have had a little luck, but now I am trying to render the right Module component based on the parameter called {num} from below? how do I do this??
App.js
<Route exact path="/" component={Menu}>
<Route exact path="/Module/:num" component={Module} />
Menu.js
export default function Menu({ match }) {
const modules = [
{
title: `Module1`,
subtitle: "Introduction",
},
{
title: `Module2`,
subtitle: "Overview",
}
];
return (
<div className={styles.menuGrid}>
{modules.map((module, index) => (
<div key={index} className={styles.menuCard}>
<Link to={`/Module/${index + 1}`}>
<h2>{module.name}</h2>
<p>{module.subtitle}</p>
</Link>
</div>
))}
</div>
);
}
Module.js
import React from "react";
import Module1 from "./Pages/Module1";
import Module2 from "./Pages/Module2";
export default function Module({ match }) {
const {
params: { num },
} = match;
return (
<div>
{`<Module${num}/>`}<---not valid but my goal??
??RENDER MODULE1 OR 2 BASED ON {num}??
</div>
);
}