Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using material-UI tabs with react

Something I stumble upon is understanding how to use Material-UI tabs. I found many posts but each target a different version of material-UI and each give a completely different way of implementation.

The web application I an creating is an analytical dashboard. I have 3 parts on the page.

  1. An Appbar
  2. main body
  3. footer

In the main body part I want to have a tabs component and dashboard component. Selecting different tab will display different dashboard component.

I started with the default Tabs component which didn’t display it right, and then I read that I need to do it using React Router, but then it depends on which router version I use.

It might be that my SPA understanding is also lacking as to why I should involve router when using Tabs.

My setup: Latest React 16.x Latest React -router 4.x Latest Material-UI 1.0.3 Using Redux as well

like image 506
user10063165 Avatar asked Jan 18 '26 11:01

user10063165


1 Answers

here is small example I created using App bar and tabs from Material-ui with react-router

<BrowserRouter>
 <div className={classes.root}>
  <AppBar position="static" color="default">
    <Tabs
      value={this.state.value}
      onChange={this.handleChange}
    >
      <Tab label="Item One" component={Link} to="/one" />
      <Tab label="Item Two" component={Link} to="/two" />
    </Tabs>
  </AppBar>

  <Switch>
    <Route path="/one" component={PageShell(ItemOne)} />
    <Route path="/two" component={PageShell(ItemTwo)} />
  </Switch>
</div>

here I have used Switch to introduce routes for the application and Link component to trigger route changes

working example: https://codesandbox.io/s/04p1v46qww

I have added transition animation for the example feel free to remove the animation if you wish.

why we use react router

That's when we want add users to go through routes urls and find the relevant pages ex: /home will render home page and /profile for profile page.

with react-router you can use many functionalities like go through history that means you can view previous pages you went with going back.

pass url params so we can render components as params changes

however If a developer wish he can design and finish an app without using routes. It will be a mess with conditional rendering but still.

from the user's view, it is a regular web app (he doesn't need to know how it is designed: SPA or otherwise) and it should work as any web app/website should work. That's what routing does same time help us to do more efficient development.

use this for tabs without using routes: https://codesandbox.io/s/qlq1j47l2w

Hope this will help you

like image 179
Nadun Avatar answered Jan 21 '26 01:01

Nadun