Issue with Material-UI tab not showing the component upon page load

After setting up material-ui tabs with react-router, I encountered an issue where only the tab names Tab A and Tab B are displayed upon page render. The desired behavior is for the TabAReport component to be automatically rendered without requiring user interaction.

Despite configuring the necessary values for the first tab to display automatically, it doesn't seem to be functioning as expected.

If anyone could offer assistance or guidance on resolving this issue, it would be greatly appreciated.

           const routes = ["/tab-a-report", "/tab-b-report"];

          <Tabs
            value={0}
            onChange={handleChange}
            textColor="default"
            variant="standard"                                                   
          >
            <Tab 
              value={0}
              label="Tab A" 
              component={Link}
              to={`${routes[0]}/${id}`}
            />
            <Tab 
              value={1}
              label="Tab B" 
              component={Link}
              to={`${routes[1]}/${id}`}
            />
          </Tabs>

        <Switch>
          <Route exact path="/tab-a-report/:id" component={TabAReport} />
          <Route exact path="/tab-b-report/:id" component={TabBReport} />
        </Switch>

Answer №1

If you want to achieve this, you need to define a route without a specific path as the last child of the Switch element. This route will be displayed if none of the other routes' paths match the current path. Check out React Router: Switch for more information.

<Switch>
    <Route exact path="/tab-a-report/:id" component={TabAReport} />
    <Route exact path="/tab-b-report/:id" component={TabBReport} />
    <Route><TabAReport id={id} /></Route> //provide the default id as a property
</Switch>

To access the :id parameter within the child component, use the useParam hook or retrieve it from the properties in the default scenario.

const { id } = useParams() || {id: props.id}

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

Implementing file uploads using graphql-upload library, integrating it with apollo-server-fastify, and taking advantage of the code-first

Is there a correct way to implement file uploads from a client to a server using the following combination of packages/techniques (along with their corresponding dependencies not listed): graphql-upload apollo-server-fastify @nestjs/platform-fastify (co ...

What is the best way to get rid of a connect-flash notification?

I'm having trouble removing the message (with the username displayed) after logging out by pressing the logout button. Every time I try to press the logout button, it just refreshes the page without any action. I want to stay on the same page and not ...

Switching minimum and maximum input values based on a specific condition: A step-by-step guide

I am looking for a way to reverse the minimum and maximum values of two input elements that I have defined. Is there a way to achieve this using HTML or JavaScript (Angular)? Here is an example of what I am trying to do: <label> Width: < ...

Guide to swapping images based on conditions using Angular JS

I am trying to display an image based on data received from an API response instead of text. If the value is true, I want to show an access symbol. If the value is false, I want to show a deny symbol. However, when attempting this, I am only getting th ...

The printing code is not able to interpret the CSS

Having trouble with this script not reading the style properly when printing, can anyone assist in identifying the issue? <script type="text/javascript"> $(function () { $("#btnPrint").click(function () { var contents = $( ...

Is there a more efficient method for integrating useParams within my context provider?

Currently, I am exploring context and working with a route: /show/:id In my context, I need to access the params ID of /show/:id. To achieve this, I initialized a state to an empty string in my context and utilized useParams in /show/:id to set the id. A ...

What could be causing AngularJS to fail to send a POST request to my Express server?

I am currently running a Node Express server on localhost that serves a page with AngularJS code. Upon pressing a button on the page, an AngularJS controller is triggered to post a JSON back to the server. However, I am facing an issue where the post requ ...

The URL overwrites the Ajax transfer type

When making an ajax call using a generated URL from a paginator script, I encountered an issue. The URL was dynamically created as shown below: "http://192.168.1.23:8000/pricing/0/999/null/?page=9" A similar link is generated on the server: "https://xxx ...

Laravel route does not receive a parameter sent via Ajax

I am currently using Laravel 5.8 and implementing a discount code system on my website. To achieve this, I attempted to send data via Ajax in the following manner: $.ajax({ type: 'POST', url: baseurl + 'discount/register', ...

Guide on leveraging Next-auth for managing multiple sessions concurrently

I am currently developing an integration tool similar to Zappier. My goal is to utilize Next-auth for connecting to multiple applications and saving their access tokens. However, I have encountered a limitation with Next-auth only allowing for one sessio ...

The expanding submenus within each menu are activated by a simple click

I am facing an issue with my menus and submenus. Whenever I click on a menu, all the associated submenus also open up. How can I make it so only the parent submenu opens? https://i.stack.imgur.com/7FDq3.png const [subMenu, setSubMenu] = useState(false) ...

"Seamless Integration: Exploring Material-UI's Compatibility with Third-Party

Is it possible to seamlessly integrate material-ui components with non-mui components without any compatibility issues? If there are compatibility issues, what are the reasons behind them? ...

`Unable to upload spreadsheet file in xlsx format`

I'm currently working on implementing a feature to export data as xlsx files. I have been successful in exporting CSV and PDF formats, but encountered issues with the xlsx format due to dynamic imports. export const exportToXlsx = async ( gridElemen ...

Masked input fails to accurately capture data

When utilizing VueJS together with the Quasar framework. An issue arises when using a masked q-input, where the value becomes incorrect after a toggle. For instance, if we start with a default value of: officeNum: 654321, mobileNum: 12345678, Then our ...

Angular 8 combined with Mmenu light JS

Looking for guidance on integrating the Mmenu light JS plugin into an Angular 8 project. Wondering where to incorporate the 'mmenu-light.js' code. Any insights or advice would be greatly appreciated. Thank you! ...

Restore the button to its original color when the dropdown menu is devoid of options

Is it possible to change the button colors back to their original state automatically when a user deselects all options from my dropdown menu? The user can either uncheck each option box individually or click on the "clear" button to clear all selections. ...

Determine the image's position in relation to its parent element while factoring in any vertical offset

Within my HTML, I have arranged two images to sit adjacent to one another. Interestingly, one image happens to be taller than the other. Despite assigning a CSS property of vertical-align: middle to both images, the result is that the shorter image appears ...

Struggling with the incorporation of Typescript Augmentation into my customized MUI theme

I'm struggling with my custom theme that has additional key/values causing TS errors when I try to use the design tokens in my app. I know I need to use module augmentation to resolve this issue, but I'm confused about where to implement it and h ...

Presenting quiz questions using javascript

Currently, I am following the learning path on javascriptissexy.com and facing a challenge with displaying quiz questions in the best way possible. As a beginner student of JavaScript, my question is about how to approach developing the behavior of a quiz ...

Select a Button to randomly choose another Button

I am currently developing a dynamic Bootstrap OnePage-Website using HTML, CSS, and JavaScript. The highlight of this website is the Team section where users can book appointments with one of three team members by clicking on a corresponding button beneat ...