Last active
August 11, 2021 03:00
-
-
Save HaNdTriX/e6469ca887242cd52b73aff06fbb8074 to your computer and use it in GitHub Desktop.
Example next.js and Material-UI next: TabNav with shared state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import Router from 'next/router' | |
import AppBarMD from 'material-ui/AppBar' | |
import Tabs, { Tab } from 'material-ui/Tabs' | |
const routes = [ | |
'/', | |
'/articles' | |
] | |
// Shared State | |
let newIndex = 0 | |
let lastIndex | |
class AppBar extends React.Component { | |
handleChange = (event, index) => { | |
lastIndex = newIndex | |
newIndex = index | |
Router.push(routes[index]) | |
}; | |
componentDidMount () { | |
if (typeof lastIndex !== 'undefined') { | |
setTimeout(() => { | |
lastIndex = undefined | |
this.forceUpdate() | |
}, 0) | |
} | |
} | |
render () { | |
const index = typeof lastIndex === 'undefined' | |
? newIndex | |
: lastIndex | |
return ( | |
<AppBarMD position='static'> | |
<Tabs index={index} onChange={this.handleChange}> | |
{routes.map((route, index) => ( | |
<Tab key={index} label={route} /> | |
))} | |
</Tabs> | |
</AppBarMD> | |
) | |
} | |
} | |
export default AppBar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sasfasfsa