Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
91
rated 0 times [  96] [ 5]  / answers: 1 / hits: 19163  / 7 Years ago, mon, december 18, 2017, 12:00:00

i'm using material ui as the mandatory library for the current
project. A page of a project requires four tabs, so I'm using the tab component from material ui library.



When I'm rendering the page which contains the tabs by default the first tab is the active tab. I want to set the fourth tab as active.



From the documentation, I see the value prop of the Tab. So I set the values of my four tabs to 1,2,3 and 4 for each Tab respectively. When Inavigate to the respective screen , i dispatch an action which is set property tab value in my store as 4.



Then though mapStateToProps i'm made this property accessible to my component. So the value when I enter the page is four but still the active Tab is the first one. Let me show you my code:



const mapStateToProps = state => ({
value: state.get('tabValue');
});

const mapDispatchToProps = dispatch => ({
tabClicked: () => setActiveTab('tabValue', 4)
})


And my component:



const Tabs = ({ value }) => (
<Tabs>
<Tab value={1}></Tab>
....
<Tab value={value}</Tab>
</Tabs


)


More From » reactjs

 Answers
28

For you to select a different tab by default use initialSelectedIndex.


For older versions of material ui, it will be of the form


<Tabs initialSelectedIndex={value}>
<Tab value={1}></Tab>
...
<Tab value={4}></Tab>
</Tabs>

For newer versions >4


<Tabs value={value}>
<Tab label="Tab1" />
<Tab label="Tab2" />
<Tab label="Tab3" />
</Tabs>

Check https://material-ui.com/components/tabs/


[#55657] Thursday, December 14, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katharinek

Total Points: 302
Total Questions: 105
Total Answers: 123

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
katharinek questions
Sat, Jan 16, 21, 00:00, 3 Years ago
Sat, Dec 5, 20, 00:00, 4 Years ago
Mon, Nov 30, 20, 00:00, 4 Years ago
Fri, Aug 21, 20, 00:00, 4 Years ago
;