React TreeView - Enhance Performance on Large Datasets

If the TreeView performance is low, consider enabling the Virtual Mode. In this mode, the TreeView loads a set of child nodes once their parent node is expanded. The Virtual Mode can be enabled only if your data source satisfies the following conditions.

To enable the Virtual Mode, set the virtualModeEnabled property to true.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeView from 'devextreme-react/tree-view';
  •  
  • const plainData = [
  • { id: 1, text: 'Fruits', parentId: -1 },
  • { id: 11, text: 'Apples', parentId: 1, hasItems: false },
  • { id: 12, text: 'Oranges', parentId: 1, hasItems: false },
  • { id: 2, text: 'Vegetables', parentId: -1 },
  • { id: 21, text: 'Cucumbers', parentId: 2, hasItems: false },
  • { id: 22, text: 'Tomatoes', parentId: 2, hasItems: false }
  • ];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeView
  • dataStructure="plain"
  • dataSource={plainData}
  • virtualModeEnabled={true}
  • rootValue={-1} />
  • );
  • }
  • }
  •  
  • export default App;

View Demo

If the Virtual Mode does not meet your requirements, you can get full control over nodes and how to load them in the createChildren function. This function will be called at the beginning of the UI component's lifetime and each time a user expands a node whose child nodes have not been loaded yet.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeView from 'devextreme-react/tree-view';
  • import 'whatwg-fetch';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeView
  • dataStructure="plain"
  • createChildren={this.createChildren} />
  • );
  • }
  •  
  • createChildren(parent) {
  • const parentId = parent ? parent.itemData.id : '';
  •  
  • return fetch(`https://mydomain.com/MyDataService?parentId=${parentId}`)
  • .then(response => response.json())
  • .catch(() => { throw 'Data Loading Error'; });
  • }
  • }
  •  
  • export default App;

View Demo

See Also