React Tabs - Control the Behavior

An end user can select Tabs items in two different modes: 'single' (by default) or 'multiple'. You can use the selectionMode property to change the mode.

App.js
  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Tabs } from 'devextreme-react/tabs';
  •  
  • const tabs = [
  • { text: "User" },
  • { text: "Comment" },
  • // ...
  • ];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Tabs
  • items={tabs}
  • selectionMode="multiple"
  • />
  • );
  • }
  • }
  •  
  • export default App;

If you need a tab to be preselected or to select it programmatically, pass its index in the data source array to the selectedIndex property.

App.js
  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Tabs } from 'devextreme-react/tabs';
  •  
  • const tabs = [
  • { text: "User" },
  • { text: "Comment" },
  • // ...
  • ];
  •  
  • class App extends React.Component {
  • constructor() {
  • this.state = {
  • selectedIndex: 1
  • }
  • }
  •  
  • handleOptionChange = (e) => {
  • if(e.fullName === 'selectedIndex') {
  • this.setState({
  • selectedIndex: e.value
  • });
  • }
  • }
  •  
  • render() {
  • return (
  • <Tabs
  • items={tabs}
  • selectedIndex={this.state.selectedIndex}
  • onOptionChanged={this.handleOptionChange}
  • />
  • );
  • }
  • }
  •  
  • export default App;

As an alternative, you can use the selectedItem (for "single" selectionMode) or selectedItems (for "multiple" selectionMode) properties.

App.js
  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Tabs } from 'devextreme-react/tabs';
  •  
  • const tabs = [
  • { text: "User" },
  • { text: "Comment" },
  • // ...
  • ];
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.state = {
  • selectedItems: [tabs[0], tabs[1]];
  • }
  • }
  •  
  • handleOptionChange = (e) => {
  • if(e.fullName === 'selectedItems') {
  • this.setState({
  • selectedItems: e.value
  • });
  • }
  • }
  •  
  • render() {
  • return (
  • <Tabs
  • items={tabs}
  • selectionMode="multiple"
  • selectedItems={this.state.selectedItems}
  • onOptionChanged={this.handleOptionChange}
  • />
  • );
  • }
  • }
  •  
  • export default App;

When the total length of all tabs exceeds the Tabs container, the UI component shows navigation buttons that help an end user scroll the tab strip. This behavior is default only for desktops. To enable it on all types of devices, assign true to the showNavButtons property. Otherwise, assign false.

App.js
  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Tabs } from 'devextreme-react/tabs';
  •  
  • const tabs = [
  • { text: "User" },
  • { text: "Comment" },
  • // ...
  • ];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Tabs
  • items={tabs}
  • showNavButtons={true}
  • />
  • );
  • }
  • }
  •  
  • export default App;
See Also