I'm currently learning how to utilize the Material-UI
library. As I explore its capabilities, I have discovered that I can use createStyles
within a component to style it, and I can also leverage createMuiTheme
to establish a global theme. My goal is to create a theme using createMuiTheme
that incorporates various primary and secondary color combinations.
My project involves developing a web application that showcases NHL team statistics. I am dynamically generating a component to display team statistics based on React-Router
and the URL of the page. For instance, visiting /rangers
will show the statistics for the New York Rangers, and visiting /bruins
will display the Boston Bruins' stats.
To achieve this, I am using React-Router's
useLocation function. By extracting the team name from the URL using useLocation
when the user navigates to a specific team page, I can make a GET request for that team's statistics to be retrieved and displayed.
My aim is to create a Material-UI
theme that dynamically adjusts the primary and secondary colors on the page based on the selected team. For example, when on the /rangers
page, I want the primary color to be blue and the secondary color to be red (representing the Rangers team colors). If the user then navigates to /bruins
, I want the primary and secondary colors to switch to the Bruins' team colors (black and gold).
const Theme = createMuiTheme({
palette: {
primary: {
// Setting primary color to Rangers blue when on '/rangers'
rangers: '#0038a8',
// Setting primary color to Bruins gold when on '/bruins'
bruins: '#fcb514'
},
secondary: {
// Setting secondary color to Rangers red when on '/rangers'
rangers: '#ce1126',
// Setting secondary color to Bruins black when on '/bruins'
bruins: '#111'
}
});
Is there a way to dynamically adjust theme colors in Material-UI
based on the current React-Router
page, such as changing the theme colors to reflect the team colors when navigating to a specific team's page like /rangers
or /bruins
? I would like to implement this using useLocation
in a similar manner to how I handle the GET request.
With a total of 31 different teams/pages to consider, dynamically managing theme colors is much more efficient than creating separate components with different styles for each team.