Content render modes in React Tab component

13 Dec 202424 minutes to read

Tabs support rendering content based on different scenarios. The content of the tabs can be rendered in three different ways, as outlined below.

On Demand rendering or lazy loading

This mode is the default, where only the content of the currently selected tab is initially loaded and available in the DOM, with subsequent tab content rendered upon selection. Once a tab’s content is loaded, it remains in the DOM. This ensures that the state of the tabs, such as scroller positions, form values, etc., is preserved.

In the following code example, the Calendar and Scheduler are rendered in the first and second tabs, respectively. Initially, the Scheduler is not available, but it will be rendered once the second tab is selected. Both the Calendar and Scheduler are maintained in the DOM.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useState } from 'react';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
import { ScheduleComponent, ViewsDirective, ViewDirective, Inject, Day } from '@syncfusion/ej2-react-schedule';
import { CalendarComponent } from '@syncfusion/ej2-react-calendars';

const ReactApp = () => {

  const [headerText] = useState([{ text: "Calendar" }, { text: "Schedule" }]);


  const calendarContent = () => {
    return (
      <div><CalendarComponent></CalendarComponent></div>
    );
  };

  const scheduleContent = () => {
    return (
      <ScheduleComponent width='100%' height='450px'>
        <ViewsDirective>
          <ViewDirective option='Day' />
          <ViewDirective option='Week' />
          <ViewDirective option='WorkWeek' />
          <ViewDirective option='Month' />
        </ViewsDirective>
        <Inject services={[Day]} />
      </ScheduleComponent>
    );
  };


  return (
    <TabComponent>
      <TabItemsDirective>
        <TabItemDirective header={headerText[0]} content={calendarContent} />
        <TabItemDirective header={headerText[1]} content={scheduleContent} />
      </TabItemsDirective>
    </TabComponent>
  );
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useState } from 'react';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
import { ScheduleComponent, ViewsDirective, ViewDirective, Inject, Day } from '@syncfusion/ej2-react-schedule';
import { CalendarComponent } from '@syncfusion/ej2-react-calendars';

const ReactApp = () => {

  const [headerText] = useState<[]>([{ text: "Calendar" }, { text: "Schedule" }]);


  const calendarContent = () => {
    return (
      <div><CalendarComponent></CalendarComponent></div>
    );
  };

  const scheduleContent = () => {
    return (
      <ScheduleComponent width='100%' height='450px'>
        <ViewsDirective>
          <ViewDirective option='Day' />
          <ViewDirective option='Week' />
          <ViewDirective option='WorkWeek' />
          <ViewDirective option='Month' />
        </ViewsDirective>
        <Inject services={[Day]} />
      </ScheduleComponent>
    );
  };


  return (
    <TabComponent>
      <TabItemsDirective>
        <TabItemDirective header={headerText[0]} content={calendarContent} />
        <TabItemDirective header={headerText[1]} content={scheduleContent} />
      </TabItemsDirective>
    </TabComponent>
  );
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);

Dynamic rendering

This mode can be applied to Tabs by setting the loadOn property to Dynamic using loadOn. In this mode, only the content of the currently selected tab is loaded and available in the DOM initially. When a different tab is selected, its content replaces the current content. Since this mode ensures the DOM maintains only the content of the active tab, page loading performance is improved. However, the Tabs do not retain their state, as each time a tab is selected, it loads its content again.

In the following code example, there are two tabs. The first tab contains a login page, and the second tab contains a Grid component. The Grid component in the second tab will only be rendered in the DOM after the login is completed. Upon successful login, the second tab will replace the first tab in the DOM.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {useRef, useState} from 'react';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { GridComponent, ColumnsDirective, ColumnDirective, Inject, Page } from '@syncfusion/ej2-react-grids';

const ReactApp = () => {
  
  const tabObj = useRef(null);  
  const userNameObj = useRef(null);
  const passWordObj = useRef(null);
  const [headerText] = useState([{ text: "Login" }, { text: "Grid" }]);
  const [userName, setUserName] = useState('');
  const [password, setPassword] = useState('');
  const [loginDisabled, setLoginDisabled] = useState(false);
  const [gridDisabled, setGridDisabled] = useState(true);

  const handleSubmit = () => {
    const userName = userNameObj.current.value;
    const password = passWordObj.current.value;
    if (!userName && !password) {
      window.alert('Enter both username and password');
    } else if (!userName) {
      window.alert('Enter the username');
    } else if (!password) {
      window.alert('Enter the password');
    } else if (userName.length < 4) {
      window.alert('Username must be at least 4 characters long');
    } else {
      setUserName('');
      setPassword('');
      setLoginDisabled(true);
      setGridDisabled(false);
    }
  };

  const loginContent = () => {
    return (
      <div className="login-form">
        <div className="wrap">
          <div id="heading">Sign in to view the Grid</div>
          <br />
          <div id="input-container">
            <TextBoxComponent placeholder="User Name" value={userName} floatLabelType="Auto" ref={userNameObj} />
            <br /><br />
            <TextBoxComponent placeholder="Password" value={password} floatLabelType="Auto" ref={passWordObj}/>
          </div>
        </div>
        <br />
        <div className="button-contain" style=>
          <ButtonComponent cssClass='e-primary' onClick={handleSubmit}>Submit</ButtonComponent>
        </div>
      </div>
    );
  };

  const gridContent = () => {
    const data = [
      { OrderID: 10248, CustomerID: 'ALFKI', OrderDate: '2024-12-01', Freight: 32.38 },
      { OrderID: 10249, CustomerID: 'ANATR', OrderDate: '2024-12-02', Freight: 11.61 },
      { OrderID: 10250, CustomerID: 'ANTON', OrderDate: '2024-12-03', Freight: 65.83 },
      { OrderID: 10251, CustomerID: 'AROUT', OrderDate: '2024-12-04', Freight: 41.34 }
    ];

    return (
      <GridComponent dataSource={data} allowPaging={true}>
        <ColumnsDirective>
          <ColumnDirective field="OrderID" headerText="Order ID" width="120" />
          <ColumnDirective field="CustomerID" headerText="Customer Name" width="130" />
          <ColumnDirective field="OrderDate" headerText="Order Date" width="120" format="yMd"/>
          <ColumnDirective field="Freight" headerText="Freight" width="120" format="C2"/>
        </ColumnsDirective>
        <Inject services={[Page]} />
      </GridComponent>
    );
  };


  return (
    <TabComponent ref={tabObj} loadOn='Dynamic'>
      <TabItemsDirective>
        <TabItemDirective header={headerText[0]} content={loginContent} disabled={loginDisabled}/>
        <TabItemDirective header={headerText[1]} content={gridContent} disabled={gridDisabled}/>
      </TabItemsDirective>
    </TabComponent>
  );
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {useRef, useState} from 'react';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { GridComponent, ColumnsDirective, ColumnDirective, Inject, Page } from '@syncfusion/ej2-react-grids';

const ReactApp = () => {
  
  const tabObj = useRef<TabComponent>(null);  
  const userNameObj = useRef<TextBoxComponent>(null);
  const passWordObj = useRef<TabComponent>(null);
  const [headerText] = useState<[]>([{ text: "Login" }, { text: "Grid" }]);;
  const [userName, setUserName] = useState<string>('');
  const [password, setPassword] = useState<string>('');
  const [loginDisabled, setLoginDisabled] = useState<Boolean>(false);
  const [gridDisabled, setGridDisabled] = useState<Boolean>(true);

  const handleSubmit = () => {
    const userName: string = userNameObj.current.value;
    const password: string = passWordObj.current.value;
    if (!userName && !password) {
      window.alert('Enter both username and password');
    } else if (!userName) {
      window.alert('Enter the username');
    } else if (!password) {
      window.alert('Enter the password');
    } else if (userName.length < 4) {
      window.alert('Username must be at least 4 characters long');
    } else {
      setUserName('');
      setPassword('');
      setLoginDisabled(true);
      setGridDisabled(false);
    }
  };

  const loginContent = () => {
    return (
      <div className="login-form">
        <div className="wrap">
          <div id="heading">Sign in to view the Grid</div>
          <br />
          <div id="input-container">
            <TextBoxComponent placeholder="User Name" value={userName} floatLabelType="Auto" ref={userNameObj} />
            <br /><br />
            <TextBoxComponent placeholder="Password" value={password} floatLabelType="Auto" ref={passWordObj}/>
          </div>
        </div>
        <br />
        <div className="button-contain" style=>
          <ButtonComponent cssClass='e-primary' onClick={handleSubmit}>Submit</ButtonComponent>
        </div>
      </div>
    );
  };

  const gridContent = () => {
    const data = [
      { OrderID: 10248, CustomerID: 'ALFKI', OrderDate: '2024-12-01', Freight: 32.38 },
      { OrderID: 10249, CustomerID: 'ANATR', OrderDate: '2024-12-02', Freight: 11.61 },
      { OrderID: 10250, CustomerID: 'ANTON', OrderDate: '2024-12-03', Freight: 65.83 },
      { OrderID: 10251, CustomerID: 'AROUT', OrderDate: '2024-12-04', Freight: 41.34 }
    ];

    return (
      <GridComponent dataSource={data} allowPaging={true}>
        <ColumnsDirective>
          <ColumnDirective field="OrderID" headerText="Order ID" width="120" />
          <ColumnDirective field="CustomerID" headerText="Customer Name" width="130" />
          <ColumnDirective field="OrderDate" headerText="Order Date" width="120" format="yMd"/>
          <ColumnDirective field="Freight" headerText="Freight" width="120" format="C2"/>
        </ColumnsDirective>
        <Inject services={[Page]} />
      </GridComponent>
    );
  };


  return (
    <TabComponent ref={tabObj} loadOn='Dynamic'>
      <TabItemsDirective>
        <TabItemDirective header={headerText[0]} content={loginContent} disabled={loginDisabled}/>
        <TabItemDirective header={headerText[1]} content={gridContent} disabled={gridDisabled}/>
      </TabItemsDirective>
    </TabComponent>
  );
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);

On initial rendering

This mode can be applied to Tabs by setting the loadOn property to Init using loadOn. In this mode, the content of all the tabs is rendered on initial load and maintained in the DOM. This mode is ideal when you have a small number of tabs and need to preserve the state of each tab. It also allows you to access the references of components rendered in other tabs.

In the following example, all three tabs are rendered on the initial load, and the data entered in the first tab will be maintained even when the second or third tab is active.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useRef, useState } from 'react';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';

const ReactApp = () => {

  const userNameObj = useRef<TextBoxComponent>(null);
  const passWordObj = useRef<TextBoxComponent>(null);
  const [headerText] = useState([{ text: "Login" }, { text: "Syncfusion EJ2" }, { text: 'FeedBack' }]);;
  const [userName, setUserName] = useState('');
  const [password, setPassword] = useState('');
  const [selectedTabIndex, setSelectedTabIndex] = useState(0);

  const handleSignIn = () => {
    const userName = userNameObj.current.value;
    const password = passWordObj.current.value;
    if (!userName && !password) {
      window.alert('Enter both username and password');
    } else if (!userName) {
      window.alert('Enter the username');
    } else if (!password) {
      window.alert('Enter the password');
    } else if (userName.length < 4) {
      window.alert('Username must be at least 4 characters long');
    }
    setUserName(userName);
    setSelectedTabIndex(1);
  };

  const handleSkip = () => {
    setSelectedTabIndex(1);
  }

  const handleSubmit = () => {
    setUserName('');
    setPassword('');
    setSelectedTabIndex(0);
  }

  const loginContent = () => {
    return (
      <div className="login-form">
        <div className="wrap">
          <div id="input-container">
            <TextBoxComponent placeholder="User Name" value={userName} floatLabelType="Auto" ref={userNameObj} />
            <br /><br />
            <TextBoxComponent placeholder="Password" value={password} floatLabelType="Auto"/>
          </div>
        </div>
        <br />
        <div className="button-contain"
          style=>
          <ButtonComponent cssClass="e-primary" onClick={handleSignIn}> SignIn </ButtonComponent>
          <ButtonComponent cssClass="e-primary" onClick={handleSkip}> Skip </ButtonComponent>
        </div>
      </div>
    );
  };

  const overViewContent = () => {
    return (
      <div className="over-view">
        <p>
          You can check out our Syncfusion Ej2 demo{' '}
          <a href="https://ej2.syncfusion.com/demos/" target="_blank" rel="noopener noreferrer">
            here
          </a>.
        </p>
        <br />
        <p>
          The user guide is available{' '}
          <a href="https://ej2.syncfusion.com/documentation/introduction" target="_blank" rel="noopener noreferrer">
            here
          </a>.
        </p>
      </div>
    );
  }

  const feedBckContent = () => {
    return (
      <div className="feed-back">
        <div className="wrap">
          <div id="input-container">
            <TextBoxComponent placeholder="Name" value={userName} floatLabelType="Auto" />
            <br /><br />
            <TextBoxComponent placeholder="Email" floatLabelType="Auto" />
            <br /><br />
            <TextBoxComponent placeholder="Comment" floatLabelType="Auto" />
          </div>
        </div>
        <br />
        <div
          className="button-contain"
          style=
        >
          <ButtonComponent cssClass="e-primary" onClick={handleSubmit}> submit </ButtonComponent>
        </div>
      </div>
    );
  };


  return (
    <TabComponent loadOn='Init' selectedItem={selectedTabIndex}>
      <TabItemsDirective>
        <TabItemDirective header={headerText[0]} content={loginContent} />
        <TabItemDirective header={headerText[1]} content={overViewContent} />
        <TabItemDirective header={headerText[2]} content={feedBckContent} />
      </TabItemsDirective>
    </TabComponent>
  );
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useRef, useState } from 'react';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';

const ReactApp = () => {

  const userNameObj = useRef<TextBoxComponent>(null);
  const passWordObj = useRef<TextBoxComponent>(null);
  const [headerText] = useState<[]>([{ text: "Login" }, { text: "Syncfusion EJ2" }, { text: 'FeedBack' }]);;
  const [userName, setUserName] = useState<string>('');
  const [password, setPassword] = useState<string>('');
  const [selectedTabIndex, setSelectedTabIndex] = useState<number>(0);

  const handleSignIn = () => {
    const userName: string = userNameObj.current.value;
    const password: string = passWordObj.current.value;
    if (!userName && !password) {
      window.alert('Enter both username and password');
    } else if (!userName) {
      window.alert('Enter the username');
    } else if (!password) {
      window.alert('Enter the password');
    } else if (userName.length < 4) {
      window.alert('Username must be at least 4 characters long');
    }
    setUserName(userName);
    setSelectedTabIndex(1);
  };

  const handleSkip = () => {
    setSelectedTabIndex(1);
  }

  const handleSubmit = () => {
    setUserName('');
    setPassword('');
    setSelectedTabIndex(0);
  }

  const loginContent = () => {
    return (
      <div className="login-form">
        <div className="wrap">
          <div id="input-container">
            <TextBoxComponent placeholder="User Name" value={userName} floatLabelType="Auto" ref={userNameObj} />
            <br /><br />
            <TextBoxComponent placeholder="Password" value={password} floatLabelType="Auto" ref={passWordObj} />
          </div>
        </div>
        <br />
        <div className="button-contain"
          style=>
          <ButtonComponent cssClass="e-primary" onClick={handleSignIn}> SignIn </ButtonComponent>
          <ButtonComponent cssClass="e-primary" onClick={handleSkip}> Skip </ButtonComponent>
        </div>
      </div>
    );
  };

  const overViewContent = () => {
    return (
      <div className="over-view">
        <p>
          You can check out our Syncfusion Ej2 demo{' '}
          <a href="https://ej2.syncfusion.com/demos/" target="_blank" rel="noopener noreferrer">
            here
          </a>.
        </p>
        <br />
        <p>
          The user guide is available{' '}
          <a href="https://ej2.syncfusion.com/documentation/introduction" target="_blank" rel="noopener noreferrer">
            here
          </a>.
        </p>
      </div>
    );
  }

  const feedBckContent = () => {
    return (
      <div className="feed-back">
        <div className="wrap">
          <div id="input-container">
            <TextBoxComponent placeholder="Name" value={userName} floatLabelType="Auto" />
            <br /><br />
            <TextBoxComponent placeholder="Email" floatLabelType="Auto" />
            <br /><br />
            <TextBoxComponent placeholder="Comment" floatLabelType="Auto" />
          </div>
        </div>
        <br />
        <div
          className="button-contain"
          style=
        >
          <ButtonComponent cssClass="e-primary" onClick={handleSubmit}> submit </ButtonComponent>
        </div>
      </div>
    );
  };


  return (
    <TabComponent loadOn='Init' selectedItem={selectedTabIndex}>
      <TabItemsDirective>
        <TabItemDirective header={headerText[0]} content={loginContent} />
        <TabItemDirective header={headerText[1]} content={overViewContent} />
        <TabItemDirective header={headerText[2]} content={feedBckContent} />
      </TabItemsDirective>
    </TabComponent>
  );
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);