Skip to content

Instantly share code, notes, and snippets.

@HaNdTriX
Last active August 11, 2021 03:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HaNdTriX/e6469ca887242cd52b73aff06fbb8074 to your computer and use it in GitHub Desktop.
Save HaNdTriX/e6469ca887242cd52b73aff06fbb8074 to your computer and use it in GitHub Desktop.
Example next.js and Material-UI next: TabNav with shared state
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
@Rahulsinghhh
Copy link

sasfasfsa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment