Ways to set Material UI tabs as active exclusively for particular URLs

Looking for a solution to address a conflict arising from the active tab indicator being located on the right tab. Within my navbar, I have tabs leading to three different routes, two of which are quite similar.

<Tabs indicatorColor="primary" value={this.state.tabsValue} onChange={this.setTabsValue}>
                    <Tab className={classes.tab}
                      label="Main"
                      value="main"
                      component={Link}
                      to="/main"
                      classes={{ selected: classes.selectedTab, }} />
                    <Tab className={classes.tab}
                      label="Shoes"
                      value="shoes"
                      component={Link}
                      to="/sale/shoes"
                      classes={{ selected: classes.selectedTab, }} />
                    <Tab className={classes.tab}
                      label="Sale"
                      value="sale"
                      component={Link}
                      to="/sale"
                      classes={{ selected: classes.selectedTab }} />
                  </Tabs>

  setTabsValue = (obj, val) => {
    this.setState({
      tabsValue: val
    });
  };

The issue arises with the tabs associated with '/sale/shoes' and '/sale'. Ideally, the 'Sale' tab should only be active for the '/sale' route. However, if a user navigates to '/sale/shoes', the Sale tab remains active. This may be due to both routes containing 'sale'. Is there a way to resolve this conflict?

Answer №1

It looks like you might consider switching to the NavLink component for easier route path matching. You can simply add the exact prop to the NavLink component to ensure an exact match with a specific URL pathname.

<Tabs
  indicatorColor="primary"
  value={this.state.tabsValue}
  onChange={this.setTabsValue}
>
  <Tab
    classes={{ selected: classes.selectedTab }}
    className={classes.tab}
    label="Home"
    value="home"
    component={NavLink}
    to="/home"
    exact
  />
  <Tab
    classes={{ selected: classes.selectedTab }}
    className={classes.tab}
    label="Products"
    value="products"
    component={NavLink}
    to="/store/products"
    exact
  />
  <Tab
    classes={{ selected: classes.selectedTab }}
    className={classes.tab}
    label="Contact"
    value="contact"
    component={NavLink}
    to="/contact"
    exact
  />
</Tabs>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble handling file uploads in Laravel via JQuery Ajax requests

When attempting to upload a CSV / Excel file and receiving it through an ajax request, the process is not functioning as anticipated. I employed a formdata object to upload the files in this manner: const formData = new FormData() formDa ...

Unable to retrieve data from a nested object within a JSON structure

In my React project, I have created a function component like the one below: function FetchData() { const [randomUserData, setRandomUserData] = useState(''); const url = 'https://randomuser.me/api/'; const fetchData = () => { ...

Looking to transition from Angular 1.5x to ReactJS, what is the most effective method for conducting A/B tests and exploring different views?

As I transition part of the UI code to ReactJS, I am considering A/B testing my app for instrumentation. I have looked into Intuit's Wasabi as a potential framework but found its setup process in production to be cumbersome. I am seeking an alternativ ...

The NodeList returned by querySelectorAll is not defined

One issue I encountered in this code snippet is that the 'questions' array is empty. I expected the 'ol li' selector to target all list items within ordered lists. Another problem I faced was the inability to assign an id to the array, ...

Applying styles to an active router-link in Vuejs: A step-by-step guide

I have a sidebar that contains multiple sidebar items like the following: <side-bar> <template slot="links"> <sidebar-item :link="{name: 'Home', icon: 'ni ni-shop text-primary', path: '/dashboard'} ...

Encountering an issue while implementing a fresh design using Material-UI Version 5 and React Version 18.01 - Specifically facing problems with the '@mui/styles' package

Hi there! I am currently working with react V.18.01 and Material-ui v.5 for my application development, but encountered an error that I need assistance with. Being a beginner developer, I would greatly appreciate a code review to help me understand the iss ...

Issue with reCAPTCHA callback in Firebase Phone Authentication not functioning as expected

I have been working on integrating phone authentication into my NextJS website. I added the reCAPTCHA code within my useEffect, but for some reason, it does not get triggered when the button with the specified id is clicked. Surprisingly, there are no erro ...

Having trouble with the mouse trail code on codepen.io

I am currently attempting to integrate this specific CodePen sample into a Hugo template called Somrat Theme. I'm facing challenges in deciding where to place the HTML file; only the 'no cursor' section should go into style.css, and I need ...

Creating a distinct header value for every $http request

I've been assigned the task of adding a unique ID for each request to all HTTP requests made by our AngularJS application for logging purposes. While this is more crucial for API calls, I'm currently working on implementing it for all kinds of re ...

navigate back to the previous tab using protractor

When I open a new tab (second), I attempt to switch back to the first tab. common.clickOpenNewSession(); //opens a new tab browser.getAllWindowHandles().then(function (handles) { var secondWindowHandle = handles[1]; var firstWindowHandle ...

Error in Mongoose validation: "Please provide a value for the 'first' field and the 'last' field."

Another user has previously posted a similar question, but I'm still struggling with my MongoDB website. I am working on a project to store names using mongoose. Here is the code for my routes and models: const router = require("express").Router(); c ...

What is the best way to use useContext in React while ensuring adherence to the rules of hooks?

I'm perplexed about integrating a login function for Google or Github within my context provider. What is the correct method to invoke a context provider in Next JS while adhering to the rules of hooks? // user.js const Context = createContext(); co ...

Create shorter nicknames for lengthy reference names within the ng-repeat loop

Is it possible to assign an alias to a long reference name in ng-repeat? Currently, I have 2 complex objects where one acts as a grouped index for the other. Although the ng-repeat template code is functioning correctly, it's getting hard to read and ...

What is the process of compiling a Vuejs single file component?

Seeking assistance with Vuejs 2 (webpack-simple template) - I am looking for a way to compile my template before rendering it. Below is the code snippet in question: App.vue <template> <div id="app"> <h1>{{ msg }}</h1> ...

Getting blocked by CORS when trying to link an AWS React application to a Node.js API hosted on AWS

Seeking assistance with resolving CORS (Cross-Origin Resource Sharing) errors in my web application. Here are the crucial details of my setup: Front-End: React version 16.14. Back-End: Node.js version 16.19 with Express.js version 4.18.1. Server Instance: ...

Encountered an error involving the SequenceExpression node type while using Jest

While adding a snapshot test to my React code, I encountered an unexpected error message: Unexpected node type: SequenceExpression (This is an error on an internal node. Probably an internal error. Location has been estimated.) The code transpiles withou ...

The Ajax form is failing to retrieve the expected PHP data

My login system uses a combination of ajax and php. The form validation is handled through javascript, with the form data then being sent to php for database checks. In the past, this method has worked well for me, but in this instance, it seems 'NOTH ...

How can I customize the appearance of the container and items in a dropdown <select> menu?

Im using contact form 7 on wordpress, i don't want to use a plugin rather use plain css/js to bring the results if possible. Something like this : https://i.stack.imgur.com/VTWRg.jpg ...

Utilize a single submit button to navigate through multiple pages dynamically using JavaScript

I would like to navigate to different rooms with just one button using JavaScript. For instance, there are three rooms: "Kitchen, Toilet, and Bedroom". How can I utilize JS to enter any of these rooms based on my selection? If I input "kitchen" in the text ...

Increase the number of table rows as you scroll downwards

Hello everyone! I've been searching online for information on how to implement infinite scrolling in a table, but all I found were tutorials using divs. Can someone please provide guidance on how to achieve this? Perhaps a sample code snippet or tuto ...