reactjsreact-tabs

Show content on tab click - React


I am really new to React and want to display 2 tabs, first tab click should display the canvas I have and second tab should display the dropdown. I have all the three in the page right now.

My page looks like this - 2 tabs , one dropdown and one canvas.

How to bind the canvas div on the first tab click and dropdown to show on the second tab click.

Edited Code:

export class Graph extends Component {
    constructor(props) {
        super(props);
        this.state = { 
            Vdata: null,
            Edata: null,
            iconData : iconsData,
            currentIndex: 0 
        }
        this._tabClick = this._tabClick.bind(this);
        this.MainGraphComponent = this.MainGraphComponent.bind(this);
        this.CustomizedGraphComponent = this.CustomizedGraphComponent.bind(this);
    }
CustomizedGraphComponent(){
    return(
        <div className={style.container} style={{ width: '100%', paddingTop:20}}>
            <DropdownButton>
                {
                   //elements
                }
            </DropdownButton>
        </div>
    )
}

MainGraphComponent(){
    <div ref={(n) => {
        this.graphContainer = n;
        return;
    }} id="container"
    style={{height: '100%', width: '100%'}}
    />
}

 _tabClick(clickedIndex) {
        const {currentIndex} = this.state.currentIndex;
        if(clickedIndex !== currentIndex) {
            this.setState({currentIndex: clickedIndex })
        }
    }


render (){
 return (

                   <div className={style.container} style={{height: '100%', width: '100%'}}>
            <Tabs
                tabs={[
                    {
                        name: 'Main Graph',
                        title: 'Main Graph',
                        component: <this.MainGraphComponent/>
                    },
                    {
                        name: 'Customized Graph',
                        title: 'Customized Graph',
                        component:<this.CustomizedGraphComponent/>
                    }
                ]}
                onTabClick={this._tabClick}

            />
        </div>

    );
 }

Solution

  • The way I did this was to render the required component based on the index of the tabs. This index is saved in the state and is changed every time a tab is clicked. Sample code

    <Tabs
        tabs=[
            {
                name: 'Main Graph',
                title: 'Main Graph',
                component: <YourGraphComponent/>
             },
             {
                 name: 'Customized Graph',
                 title: 'Customized Graph',
                 component: <YourCustomizedGraphComponent/>
             }
            ]
            /*
              Assuming Tabs is a component, no need to specify a click handler unless
              you want to add some custom handling.
              onTabClick={this._onSelect}
            */
    />
    

    and then in you tabs component

    this.state={
        currentIndex: 0 //or get the default from the parent
    }
    
    _tabClick(clickedIndex) {
        const {currentIndex} = this.state;
        if(clickedIndex !== currentIndex) {
            this.setState({currentIndex: clickedIndex });
        }
    }
    
    render() {
      return (<div>
          {/*Basic code to render the tabs and attach click handlers to it based on index*/}
          {this.props.tabs.map((tab, index) => {
            return (<div onClick={this._tabClick.bind(index)}>
                tab.label
            </div>);
          })}
          {/*component fetched from the tabs array based on the current index*/}
          {this.props.tabs[this.state.currentIndex].component}
      </div>);
      }
    }