I am creating a dashboard in React using Typescript and Material UI which has months and a corresponding value shown besides the months. For this, I am using a React component that I have created "Dashboard". Upon clicking a particular month, I want to open another view (component) to show the user some different information. How can I do that? This is the code of my component till now. Specifically I would like to know how to update the handleMonthClick
method to open a new view when a month is selected.
import React from 'react';
import MonthCard from './MonthCard';
const Dashboard: React.FC = () => {
const months = [
{ month: 'January', passRate: 80 },
{ month: 'February', passRate: 75 },
// Add more months as needed
];
const handleMonthClick = (month: string) => {
console.log(`Clicked on ${month}`);
// You can add more logic here
};
return (
<div>
<h1>Dashboard</h1>
{months.map((m) => (
<MonthCard
key={m.month}
month={m.month}
passRate={m.passRate}
onClick={() => handleMonthClick(m.month)}
/>
))}
</div>
);
};
export default Dashboard;
2
Answers
You could create a state variable, set that state variable to the selected month, and then render some other component based on the month that is selected.
For example:
Now create a new component
MyMonthDisplay.jsx
that displays information for the passed inmonth
parameter.Yeah you can easily do that and get the desired functionality mentioned in the comments (having the new component replace the entire screen)
Below is code to do this with all the components in a single file(for example purposes), you can copy and paste this into a component folder to use it anywhere, and refer to it to update your code! (I added some super basic styles/css to make it an easier UI)
let me know if you have any questions, if you’re using tsx this should work on copy-paste, no extra libs were used