Created
August 30, 2017 09:48
-
-
Save tgrrr/c5c63c175c5c8f3fb63b929f19615ca1 to your computer and use it in GitHub Desktop.
Material Tabs that control Router 4
This file contains 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 {Tabs, Tab} from 'material-ui/Tabs'; | |
import Slider from 'material-ui/Slider'; | |
import { withRouter } from 'react-router'; | |
import { BrowserRouter as Router, Route, withRouter } from 'react-router-dom' | |
class TabsRouter extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
tabSelected: 'one', | |
}; | |
this.handleChange = this.handleChange.bind(this); | |
} | |
handleChange = (tabSelected) => { | |
this.setState({ | |
tabSelected: tabSelected, | |
}); | |
this.props.history.push(tabSelected); | |
}; | |
render() { | |
return ( | |
<Tabs | |
value={this.state.tabSelected} | |
onChange={this.handleChange} | |
> | |
<Tab label="Item One" value="one"> | |
Tab Content 1 | |
</Tab> | |
<Tab label="Item Two" value="two"> | |
Tab Content 2 | |
</Tab> | |
<Tab label="Item Two" value="three"> | |
Tab Content 3 | |
</Tab> | |
</Tabs> | |
) | |
}; | |
export default withRouter(TabsRouter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to add the case when your content is rendered conditionally depending on the path (I mean using something as react-router's "Route" component), usually the first time the tabs component gets rendered, you should append the active tab value to location.pathname in order for the route to match, e.g.
The problem is onChange does not fires in the first render, so the path could not be updated with the active tab value.
Maybe there is an easier solution, or I'm seeing a problem where there is any, but I solve implementing componentWillReceiveProps like this:
Oh! and also it's important to set tabSelected state on push, so we could navigate <- . ->
And finally a reducer for LOCATION_CHANGE action types which sets the state stored for every path