React Materail TabPanel - reactjs

I'm new to React and made the code below
Where I'm trying to insert a TabPanel but it's getting an error
Uncaught TypeError: No TabContext provided at TabPanel (TabPanel.js:26)
at renderWithHooks (react-dom.development.js:14803)
How do I adjust this error?
import React, {useState, useEffect} from "react";
import TextField from '#material-ui/core/TextField';
import Button from '#material-ui/core/Button';
import MenuItem from '#material-ui/core/MenuItem';
import Fetch from "../../../services/Fetch";
import InputDatePicker from "../../../components/InputDatepicker";
import CurrencyTextField from '#unicef/material-ui-currency-textfield';
import bridge from "./bridge";
import Tabs from '#material-ui/core/Tabs';
import Tab from '#material-ui/core/Tab';
import TabPanel from '#material-ui/lab/TabPanel';
export default (props)=>{
const [state, setState] = useState(bridge.getForm(props.idUsidUser));
const [value, setValue] = useState(0);
useEffect(()=>{
setTimeout(()=>{
var elements = document.querySelectorAll("div[min='0']");
elements.forEach((element) => {
if(element.children.length > 0){
element.children[0].min = 0;
}
});
}, 300);
}, []);
bridge.setForm = setState;
const closeModal = (event) =>{
document.querySelector("#form").removeAttribute("style");
document.querySelector("html").removeAttribute("style");
document.querySelector(".modal-overlay").removeAttribute("style");
}
const onInputChange = (event) => {
setState({
...state,
[event.target.name]: event.target.value
});
}
const handleChange = (event, newValue) => {
setValue(newValue);
};
function a11yProps(index) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
}
return (
<form onSubmit={onSubmit}>
<h3 className="featured">Create</h3>
<Tabs value={value} onChange={handleChange} aria-label="simple tabs example">
<Tab label="Tab 1" {...a11yProps(0)} />
<Tab label="Tab 2" {...a11yProps(1)} />
<Tab label="Tab 3" {...a11yProps(2)} />
<Tab label="Tab 4" {...a11yProps(3)} />
<Tab label="Tab 5" {...a11yProps(4)} />
</Tabs>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
</form>
);
}
I don't know what I did wrong for React to acknowledge the error.
What to do in this case? Well, I followed the basic principles that I studied in the framework documentation

Comments were a little confusing for me, so here's a clear answer for others who run into this:
The error comes when using the Material UI Basic Tabs demo. They use a 'TabPanel' component in this, but as defined by their function as shown in the full code of their example (see snippet below)
The other type of TabPanel is part of the MUI labs and is imported from MUI directly:
import TabPanel from '#mui/lab/TabPanel';
But that won't work with the code in their demo example.
Demo link: https://mui.com/material-ui/react-tabs/
So if you are using the demo on the page quoted, you also need to use the same function they use:
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{ p: 3 }}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}

Related

MUI Nav Tab with NextJS router - adding icon results in undefined TypeError

I've implemented NavTabs similar to the implementation found here, but using next/router.
Problem: The app crashes if you add an icon to any Tab.
Live Codesandbox reproduction
In the Codesandbox, clicking either tab will cause the error. If you then remove the icon property from both Tab components, it will work without issue.
The full code and MUI documentation can be found below.
Additional detail and logging errors:
If you were to use no label and only an icon, like this:
import HomeIcon from '#mui/icons-material/Home'
<LinkTab href="/drafts" icon = {<HomeIcon />} />
Then a click can result in a TypeError thrown at router.push(e.target.href). The same error occurs if you use all three of label, icon, and aria-label.
In the handleTabChange method, I logged the event and found what's changing.
The event from a successful click on a LinkTab with no icon:
SyntheticBaseEvent {_reactName: 'onClick', _targetInst: null, type: 'click', nativeEvent:
PointerEvent, target: a.MuiButtonBase-root.MuiTab-root.MuiTab-textColorPrimary.css-knz1ty-
MuiButtonBase-root-MuiTab-root, …}
The event from an unsuccessful click on a LinkTab with an icon:
SyntheticBaseEvent {_reactName: 'onClick', _targetInst: null, type: 'click', nativeEvent:
PointerEvent, target: path, …}
It seems that the target changes to path sometimes, and as far as I can tell this only happens when an icon is present in the LinkTab.
Any ideas how I can get around this?
Alternative attempt #1:
The below implementation updates the state of the Tabs without crashing, but it won't actually navigate to a new page. If you try to add onClick to the individual Tab components, then the Tabs state won't update as expected.
<Tabs value = {value} onChange = {(e, v) => handleChange(e, v)} aria-label = 'nav'>
<Tab icon = {<HomeIcon />} aria-label = 'home' value = {pages[0]} to = {pages[0]} component = {Link} />
<Tab icon = {<AboutIcon />} aria-label = 'about' value = {pages[1]} to = {pages[1]} component = {Link} />
</Tabs>
Alternative attempt #2:
The below implementation also results in the same TypeError:
import TabContext from '#mui/lab/TabContext'
import TabList from '#mui/lab/TabList'
<TabContext value = {value}>
<TabList value = {value} onChange = {(e, v) => handleChange(e, v)} centered aria-label = 'nav'>
<LinkTab icon = {<HomeIcon />} href = {pages[0]} aria-label = 'home' />
<LinkTab icon = {<InfoIcon />} href = {pages[1]} aria-label = 'about' />
</TabList>
</TabContext>
Error log:
Uncaught TypeError: Cannot read properties of undefined (reading 'auth')
at Object.formatUrl (format-url.js?7b53:32:11)
at Object.formatWithValidation (utils.js?e7ff:109:28)
at resolveHref (router.js?8684:190:69)
at prepareUrlAs (router.js?8684:247:38)
at Router.push (router.js?8684:532:26)
at Object.instance.<computed> [as push] (router.js?31fc:150:20)
at handleTabChange (NavTabs.js?141c:61:12)
at onClick (NavTabs.js?141c:69:25)
at handleClick (Tab.js?4f19:161:1)
...
Full code (also found on Codesandbox):
index.js:
import { useRouter } from "next/router";
import { useState } from "react";
import Box from "#mui/material/Box";
import Tabs from "#mui/material/Tabs";
import Tab from "#mui/material/Tab";
import { styled } from "#mui/material/styles";
import HomeIcon from "#mui/icons-material/Home";
import InfoIcon from "#mui/icons-material/Info";
const StyledTab = styled(Tab)(({ theme }) => ({
color: theme.palette.text.primary,
"&:hover": { color: theme.palette.primary.main }
}));
const LinkTab = (props) => {
const router = useRouter();
const handleTabChange = (e) => {
console.log(e);
e.preventDefault();
router.push(e.target.href);
};
return (
<StyledTab
component="a"
disableFocusRipple
disableRipple
onClick={(e) => handleTabChange(e)}
{...props}
/>
);
};
export const NavTabs = () => {
const router = useRouter();
const pages = ["/", "/about"];
const [value, setValue] = useState(pages.indexOf(router.route));
const handleChange = (e, v) => {
setValue(v);
};
return (
<Box>
<Tabs value={value} onChange={(e, v) => handleChange(e, v)} centered>
<LinkTab
label="Home"
href={pages[0]}
icon={<HomeIcon />}
aria-label="home"
/>
<LinkTab
label="About"
href={pages[1]}
icon={<InfoIcon />}
aria-label="about"
/>
</Tabs>
</Box>
);
};
export default () => (
<div>
<NavTabs />
</div>
);
about.js:
import NavTabs from "./index";
export default () => (
<div>
<NavTabs />
</div>
);
MUI documentation:
https://mui.com/components/tabs/#api
https://mui.com/components/tabs/#nav-tabs
https://mui.com/components/tabs/#icon-tabs
https://mui.com/components/tabs/#experimental-api
https://mui.com/components/tabs/#third-party-routing-library
https://mui.com/guides/routing/#tabs
https://mui.com/guides/routing/#next-js

How to display elements in JSX based on OnChange of react-select element

I am new to react and self-taught, struggling with state and react-select
I have a dropdown with react-select. Depending on what value the user selects I want to display the relevant data onto the screen. The data is coming from the authContext(useContext). This is what I have written so far. But its not working. Can someone please guide me in the right direction:
import React, { useContext, useState } from 'react'
import styles from './FullRecord.module.css'
import {AuthContext} from '../../shared/context/auth-context'
import Select from 'react-select'
import { makeStyles } from '#material-ui/core/styles';
import Card from '#material-ui/core/Card';
import CardContent from '#material-ui/core/CardContent';
import Typography from '#material-ui/core/Typography';
const useStyles = makeStyles({
custom: {
backgroundColor: "#558d6b",
fontWeight: "bold"
},
customFont: {
fontWeight: "bold",
fontSize: "20px"
},
customFont1: {
fontWeight: "light"
}
});
const FullRecord = (props) => {
const auth = useContext(AuthContext)
const classes = useStyles();
const [selectedValue, setSelectedValue] = useState('');
const [tableValue, setTableValue] = useState(false)
let data
const options = auth.tournaments.map((tournament) => {
return {
value: tournament.TournamentName,
label: tournament.TournamentName,
}
})
const handleChange = (selectedValue) => {
setSelectedValue(selectedValue);
setTableValue(true)
const {value} = selectedValue
let tname
if(value === 'GSM Edition 1'){
const noOfMatches = auth.profile.MemberMatches.filter((match) => match.TournamentName === 'GSM Edition 1')
if(tableValue){
return (
<div>
<li className={styles['member-item']}>
<Card className={classes.custom} variant="outlined">
<CardContent>
<Typography className={classes.customFont} gutterBottom>
Number Of Matches Played
</Typography>
<Typography className={classes.customFont}>
{noOfMatches}
</Typography>
</CardContent>
</Card>
</li>
</div>
)
}
}
}
return (
<React.Fragment>
<div className={styles['fullrecord__maindiv']}>
<Select
onChange={handleChange}
options={options}
/>
</div>
</React.Fragment>
)
}
export default FullRecord
I would say that your first problem stems from using a function argument name that is the same as your state variable. Especially a problem as the function is also an arrow function.
const handleChange = (newValue) => {
setSelectedValue(newValue);
setTableValue(true);
}
You're also trying to return JSX from your event handler, where it seems like what you really want is the core content to change when your selection changes. Ultimately, that logic goes in to your FullRecord return statement, and will automatically update as state is updated.
const FullRecord = (props) => {
// ...bunch of stuff, then
return (
<React.Fragment> {/* really don't need here, as you only have one root element */}
<div className={styles['fullrecord__maindiv']}>
<Select
onChange={handleChange}
options={options}
/>
{selectedValue ? (
{/* output something here */}
) : null}
</div>
</React.Fragment>
)
}

Unable to display an API data using map function on Material UI tabs

I'm new to this programming world. Can anyone please help me on this.
I have implemented Material UI's tabs successfully by hard-coding the content, but when I tried to make my hard coded tabs with a .map function to populate the content from a data source (json), it no longer works. The tab displays nothing.
Here are the codes,
Planet component:
import React from 'react';
function Planet(props) {
return (
<ul>
<li>{props.name}</li>
</ul>
);
}
export default Planet;
Planets component:
import React, { useEffect, useState} from 'react';
import Planet from './Planet';
function Planets(props) {
const [planets, setPlanets] = useState([]);
useEffect(() => {
getPlanets();
}, []);
const getPlanets = async () => {
const response = await fetch("https://assignment-machstatz.herokuapp.com/planet");
const data = await response.json();
setPlanets(data);
}
return (
<div>
{planets.map((planet, index) => {
return (
<Planet key={index} name={planet.name} />
);
})}
</div>
);
}
export default Planets;
App component:
import React, { useState } from 'react';
import { AppBar, Tabs, Tab } from '#material-ui/core';
import Planet from './Planet';
import Favplanets from './Favplanets';
function App() {
const [selectedTab, setSelectedTab] = useState(0);
function handleChange (event, newValue) {
setSelectedTab(newValue);
}
return (
<>
<AppBar position="static">
<Tabs value={selectedTab} onChange={handleChange} >
<Tab label="Planets" />
<Tab label="Favourite Planets" />
</Tabs>
</AppBar>
{selectedTab === 0 && <Planet />}
{selectedTab === 1 && <Favplanets />}
</>
);
}
export default App;
Thanks for your help!

Material-ui: Button in a tab list

I have a set of tabs that I want to be followed by an add button (to add a new tab). I just added a button to the children of the Tabs component and it renders and works exactly how I want it to, but there are so many warnings in the developer console.
<AppBar position="static">
<Tabs
value={this.props.selectedTab}
onChange={this.handleTabSelect}
textColor="secondary"
>
{this.props.ListOfStuff.map(el => {
return (
<Tab
className={classes.tabButton}
value={el.ClientId}
label={el.Label}
id={el.ClientId}
aria-controls={"tabPanel-" + el.ClientId}
key={"tab-" + el.ClientId}
/>
);
})}
<Button
className={classes.addButton}
onClick={this.addNewTab}
>
<AddIcon color="secondary" className={classes.addIcon} />
NEW TAB
</Button>
</Tabs>
</AppBar>
warnings like:
- Warning: Received false for a non-boolean attribute indicator.
- Warning: React does not recognize the textColor prop on a DOM element.
- Warning: ForwardRef(InputBase) contains an input of type time with both value and defaultValue props.
Any suggestions on how to keep it rendering in the same way but to get rid of all of these warnings?
Thanks
The Tabs component clones its child elements (presumed to be Tab elements) in order to pass additional properties (e.g. properties related to the "selected" tab).
The warnings are caused by these additional properties being passed to the Button component. You can fix these warnings by wrapping Button in a component that ignores the additional properties passed by Tabs such as the following:
const ButtonInTabs = ({ className, onClick, children }) => {
return <Button className={className} onClick={onClick} children={children} />;
};
Full working example:
import React from "react";
import PropTypes from "prop-types";
import { makeStyles } from "#material-ui/core/styles";
import AppBar from "#material-ui/core/AppBar";
import Tabs from "#material-ui/core/Tabs";
import Tab from "#material-ui/core/Tab";
import Typography from "#material-ui/core/Typography";
import Box from "#material-ui/core/Box";
import Button from "#material-ui/core/Button";
import AddIcon from "#material-ui/icons/Add";
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired
};
function a11yProps(index) {
return {
id: `simple-tab-${index}`,
"aria-controls": `simple-tabpanel-${index}`
};
}
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper
},
addButton: {
color: "white"
}
}));
const ButtonInTabs = ({ className, onClick, children }) => {
return <Button className={className} onClick={onClick} children={children} />;
};
export default function SimpleTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const [showThirdTab, setShowThirdTab] = React.useState(false);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs
value={value}
onChange={handleChange}
aria-label="simple tabs example"
>
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
{showThirdTab && <Tab label="Item Three" {...a11yProps(2)} />}
<ButtonInTabs
onClick={() => setShowThirdTab(true)}
className={classes.addButton}
>
<AddIcon color="secondary" />
New Tab
</ButtonInTabs>
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
</div>
);
}

Material-UI's Tabs integration with react router 4?

The new react-router syntax uses the Link component to move around the routes. But how could this be integrated with material-ui?
In my case, I'm using tabs as the main navigation system, So in theory I should have something like this:
const TabLink = ({ onClick, href, isActive, label }) =>
<Tab
label={label}
onActive={onClick}
/>
export default class NavBar extends React.Component {
render () {
return (
<Tabs>
<Link to="/">{params => <TabLink label="Home" {...params}/>}</Link>
<Link to="/shop">{params => <TabLink label="shop" {...params}/>}</Link>
<Link to="/gallery">{params => <TabLink label="gallery" {...params}/>}</Link>
</Tabs>
)
}
}
But when it renders, material-ui throws an error that the child of Tabs must be a Tab component. What could be the way to proceed? How do I manage the isActive prop for the tab?
Thanks in advance
Another solution (https://codesandbox.io/s/l4yo482pll) with no handlers nor HOCs, just pure react-router and material-ui components:
import React, { Fragment } from "react";
import ReactDOM from "react-dom";
import Tabs from "#material-ui/core/Tabs";
import Tab from "#material-ui/core/Tab";
import { Switch, Route, Link, BrowserRouter, Redirect } from "react-router-dom";
function App() {
const allTabs = ['/', '/tab2', '/tab3'];
return (
<BrowserRouter>
<div className="App">
<Route
path="/"
render={({ location }) => (
<Fragment>
<Tabs value={location.pathname}>
<Tab label="Item One" value="/" component={Link} to={allTabs[0]} />
<Tab label="Item Two" value="/tab2" component={Link} to={allTabs[1]} />
<Tab
value="/tab3"
label="Item Three"
component={Link}
to={allTabs[2]}
/>
</Tabs>
<Switch>
<Route path={allTabs[1]} render={() => <div>Tab 2</div>} />
<Route path={allTabs[2]} render={() => <div>Tab 3</div>} />
<Route path={allTabs[0]} render={() => <div>Tab 1</div>} />
</Switch>
</Fragment>
)}
/>
</div>
</BrowserRouter>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
My instructor helped me with using React Router 4.0's withRouter to wrap the Tabs component to enable history methods like so:
import React, {Component} from "react";
import {Tabs, Tab} from 'material-ui';
import { withRouter } from "react-router-dom";
import Home from "./Home";
import Portfolio from "./Portfolio";
class NavTabs extends Component {
handleCallToRouter = (value) => {
this.props.history.push(value);
}
render () {
return (
<Tabs
value={this.props.history.location.pathname}
onChange={this.handleCallToRouter}
>
<Tab
label="Home"
value="/"
>
<div>
<Home />
</div>
</Tab>
<Tab
label="Portfolio"
value="/portfolio"
>
<div>
<Portfolio />
</div>
</Tab>
</Tabs>
)
}
}
export default withRouter(NavTabs)
Simply add BrowserRouter to index.js and you're good to go.
The error you are seeing from material-ui is because it expects to have a <Tab> component rendered as direct child of <Tabs> component.
Now, here is a way that I've found to integrate the link into the <Tabs> component without loosing the styles:
import React, {Component} from 'react';
import {Tabs, Tab} from 'material-ui/Tabs';
import {Link} from 'react-router-dom';
export default class MyComponent extends Component {
render() {
const {location} = this.props;
const {pathname} = location;
return (
<Tabs value={pathname}>
<Tab label="First tab" containerElement={<Link to="/my-firs-tab-view" />} value="/my-firs-tab-view">
{/* insert your component to be rendered inside the tab here */}
</Tab>
<Tab label="Second tab" containerElement={<Link to="/my-second-tab-view" />} value="/my-second-tab-view">
{/* insert your component to be rendered inside the tab here */}
</Tab>
</Tabs>
);
}
}
To manage the 'active' property for the tabs, you can use the value property in the <Tabs> component and you also need to have a value property for each tab, so when both of the properties match, it will apply the active style to that tab.
Solution with Tab highlight, Typescript based and works well with react-route v5:
Explanation: <Tab/> here work as a link to React router. Values in <Tab/> to={'/all-event'} and value={'/all-event'} should be same in order to highlgiht
import { Container, makeStyles, Tab, Tabs } from '#material-ui/core';
import React from 'react';
import {
Link,
Route,
Switch,
useLocation,
Redirect,
} from 'react-router-dom';
import AllEvents from './components/AllEvents';
import UserEventsDataTable from './components/UserEventsDataTable';
const useStyles = makeStyles(() => ({
container: {
display: 'flex',
justifyContent: 'center',
},
}));
function App() {
const classes = useStyles();
const location = useLocation();
return (
<>
<Container className={classes.container}>
<Tabs value={location.pathname}>
<Tab
label='All Event'
component={Link}
to={`/all-event`}
value={`/all-event`}
/>
<Tab
label='User Event'
component={Link}
to={`/user-event`}
value={`/user-event`}
/>
</Tabs>
</Container>
<Switch>
<Route path={`/all-event`}>
<AllEvents />
</Route>
<Route path={`/user-event`}>
<UserEventsDataTable />
</Route>
<Route path={`/`}>
<Redirect from='/' to='/all-event' />
</Route>
</Switch>
</>
);
}
export default App;
Here's another solution, using the beta of Material 1.0 and adding browser Back/Forward to the mix:
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import AppBar from 'material-ui/AppBar';
import Tabs, { Tab } from 'material-ui/Tabs';
import { withRouter } from "react-router-dom";
import Home from "./Home";
import Portfolio from "./Portfolio";
function TabContainer(props) {
return <div style={{ padding: 20 }}>{props.children}</div>;
}
const styles = theme => ({
root: {
flexGrow: 1,
width: '100%',
marginTop: theme.spacing.unit * 3,
backgroundColor: theme.palette.background.paper,
},
});
class NavTabs extends React.Component {
state = {
value: "/",
};
componentDidMount() {
window.onpopstate = ()=> {
this.setState({
value: this.props.history.location.pathname
});
}
}
handleChange = (event, value) => {
this.setState({ value });
this.props.history.push(value);
};
render() {
const { classes } = this.props;
const { value } = this.state;
return (
<div className={classes.root}>
<AppBar position="static" color="default">
<Tabs
value={value}
onChange={this.handleChange}
scrollable
scrollButtons="on"
indicatorColor="primary"
textColor="primary"
>
<Tab label="Home" value = "/" />
<Tab label="Portfolio" value = "/portfolio"/>
</Tabs>
</AppBar>
{value === "/" && <TabContainer>{<Home />}</TabContainer>}
{value === "/portfolio" && <TabContainer>{<Portfolio />}</TabContainer>}
</div>
);
}
}
NavTabs.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withRouter(withStyles(styles)(NavTabs));
You can use browserHistory instead of React-Router Link component
import { browserHistory } from 'react-router'
// Go to /some/path.
onClick(label) {
browserHistory.push('/${label}');
}
// Example for Go back
//browserHistory.goBack()
<Tabs>
<Tab
label={label}
onActive={() => onClick(label)}
/>
</Tabs>
As you see you can simply push() your target to the browserHistory
As #gkatchmar says you can use withRouter high-order component but you can also use context API. Since #gkatchmar showed withRouter already I will only show context API. Bear in mind that this is an experimental API.
https://stackoverflow.com/a/42716055/3850405
import React, {Component} from "react";
import {Tabs, Tab} from 'material-ui';
import * as PropTypes from "prop-types";
export class NavTabs extends Component {
constructor(props) {
super(props);
}
static contextTypes = {
router: PropTypes.object
}
handleChange = (event: any , value: any) => {
this.context.router.history.push(value);
};
render () {
return (
<Tabs
value={this.context.router.history.location.pathname}
onChange={this.handleChange}
>
<Tab
label="Home"
value="/"
>
<div>
<Home />
</div>
</Tab>
<Tab
label="Portfolio"
value="/portfolio"
>
<div>
<Portfolio />
</div>
</Tab>
</Tabs>
)
}
}
Here's a simple solution using the useLocation hook. No state needed. React router v5 though.
import { Tab, Tabs } from '#material-ui/core';
import { matchPath, NavLink, useLocation } from 'react-router-dom';
const navItems = [
{
id: 'one',
path: '/one',
text: 'One',
},
{
id: 'two',
path: '/two',
text: 'Two',
},
{
id: 'three',
path: '/three',
text: 'Three',
},
];
export default function Navigation() {
const { pathname } = useLocation();
const activeItem = navItems.find((item) => !!matchPath(pathname, { path: item.path }));
return (
<Tabs value={activeItem?.id}>
{navItems.map((item) => (
<Tab key={item.id} value={item.id} label={item.text} component={NavLink} to={item.path} />
))}
</Tabs>
);
}
<BrowserRouter>
<div className={classes.root}>
<AppBar position="static" color="default">
<Tabs
value={this.state.value}
onChange={this.handleChange}
indicatorColor="primary"
textColor="primary"
fullWidth
>
<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>
I've created this hook to help control the tabs and generate the default value that catches from the location URL.
const useTabValue = (array, mainPath = "/") => {
const history = useHistory();
const { pathname } = useLocation();
const [value, setValue] = useState(0);
const pathArray = pathname.split("/");
function handleChange(_, nextEvent) {
setValue(nextEvent);
history.push(`${mainPath}/${array[nextEvent]}`);
}
const findDefaultValue = useCallback(() => {
return array.forEach((el) => {
if (pathArray.indexOf(el) > 0) {
setValue(array.indexOf(el));
return;
}
});
}, [pathArray, array]);
useEffect(() => {
findDefaultValue();
}, [findDefaultValue]);
return {
handleChange,
value,
};
};
then I have used it like this :
const NavigationBar = () => {
const classes = useStyles();
const allTabs = useMemo(() => ["home", "search"]);
const { handleChange, value } = useTabValue(allTabs, "/dashboard");
return (
<div className={classes.navBarContainer}>
<Tabs
centered
value={value}
variant="fullWidth"
onChange={handleChange}
className={classes.navBar}
>
<Tab color="textPrimary" icon={<HomeIcon />} />
<Tab color="textPrimary" icon={<ExploreIcon />} />
</Tabs>
</div>
);
};
I solved this in a much easier fashion (I was surprised this worked so well - maybe there's a problem I haven't found out). I'm using Router 6 and React 17 (I know these packages are newer).
In any case, I just used the useNavigate hook in the handleChange function. Thus, now there is NO need for Switch and the code becomes much simpler. See below:
let navigate = useNavigate();
const [selection, setSelection] = useState();
const handleChange = (event, newValue) => {
setSelection(newValue);
navigate(`${newValue}`);
}
return (
<Tabs value={selection} onChange={handleChange}>
<Tab label="Products" value="products" />
<Tab label="Customers" value="customers" />
<Tab label="Invoices" value="invoices" />
</Tabs>
);
}
The handleChange function updates 'selection' which controls the display of the tabs, and also navigates to the right path.
if you set the component somewhere in your React space, and set correctly a :style route (as explained by React Router: https://reactrouter.com/docs/en/v6/getting-started/overview), you can also control in which area of the page will the content be rendered. Hope it helps somebody!
I got it working this way in my app:
import React, {useEffect, useRef} from 'react';
import PropTypes from 'prop-types';
import {makeStyles} from '#material-ui/core/styles';
import AppBar from '#material-ui/core/AppBar';
import Tabs from '#material-ui/core/Tabs';
import Tab from '#material-ui/core/Tab';
import Typography from '#material-ui/core/Typography';
import Box from '#material-ui/core/Box';
import Container from "#material-ui/core/Container";
import {Link} from "react-router-dom";
import MenuIcon from "#material-ui/icons/Menu";
import VideoCallIcon from "#material-ui/icons/VideoCall";
const docStyles = makeStyles(theme => ({
root: {
display: 'flex',
'& > * + *': {
marginLeft: theme.spacing(2),
},
},
appBarRoot: {
flexGrow: 1,
},
headline: {
marginTop: theme.spacing(2),
},
bodyCopy: {
marginTop: theme.spacing(1),
fontSize: '1.2rem',
},
tabContents: {
margin: theme.spacing(3),
},
}));
function TabPanel(props) {
const {children, value, index, classes, ...other} = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Container>
<Box className={classes.tabContents}>
{children}
</Box>
</Container>
)}
</div>
);
}
function a11yProps(index) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
}
function TabOneContents(props) {
const {classes} = props;
return (
<>
<Typography variant="h4" component={'h1'} className={classes.headline}>
Headline 1
</Typography>
<Typography variant="body1" className={classes.bodyCopy}>
Body Copy 1
</Typography>
</>
)
}
function TabTwoContents(props) {
const {classes} = props;
const nurseOnboardingPath = '/navigator/onboarding/' + Meteor.userId() + '/1';
return (
<>
<Typography variant="h4" component={'h1'} className={classes.headline}>
Headline 2
</Typography>
<Typography variant="body1" className={classes.bodyCopy}>
Body Copy 2
</Typography>
</>
)
}
export default function MUITabPlusReactRouterDemo(props) {
const {history, match} = props;
const propsForDynamicClasses = {};
const classes = docStyles(propsForDynamicClasses);
const [value, setValue] = React.useState(history.location.pathname.includes('/tab_2') ? 1 : 0);
const handleChange = (event, newValue) => {
setValue(newValue);
const pathName = '/' + (value == 0 ? 'tab_1' : 'tab_2');
history.push(pathName);
};
return (
<div className={classes.appBarRoot}>
<AppBar position="static" color="transparent">
<Tabs value={value} onChange={handleChange} aria-label="How It Works" textColor="primary">
<Tab label="Tab 1" {...a11yProps(0)} />
<Tab label="Tab 2" {...a11yProps(1)} />
</Tabs>
</AppBar>
<TabPanel value={value} index={0} classes={classes}>
<TabOneContents classes={classes}/>
</TabPanel>
<TabPanel value={value} index={1} classes={classes}>
<TabTwoContents classes={classes}/>
</TabPanel>
</div>
);
}
...and in React Router:
[.....]
<Route exact path="/tab_1"
render={(routeProps) =>
<MUITabPlusReactRouterDemo history={routeProps.history}
/>
}/>
<Route exact path="/tab_2"
render={(routeProps) =>
<MUITabPlusReactRouterDemo history={routeProps.history} />
}/>
[.....]

Resources