Often the tree has a big structure and it is not good to load immediately the whole tree items. The library has only 2 ways to load a data from a server, but this is quite enough for any, even the most complex requirements. See below for more details on each of them.
Data initializing
Note that there is no data initialization parameter or something like that. Only data property. Let's examine how is elementary to load a data from the server.
import React from'react'import EyzyTree from'eyzy-tree'import AwesomeLoadingIndicator from'awesome-loading-indicator'import'eyzy-tree/style.css'exportdefault LazyTree extends React.Component {// https://babeljs.io/docs/en/babel-plugin-proposal-class-properties// instead you can use constructor :) state = { treeData: [], isDataLoaded:false }componentDidMount() {fetch('/awesome_API/v1/?tree').then(response =>response.json()).then(data => {this.setState({ isDataLoaded:true, treeData: data }) }) }render() {if (!this.state.isDataLoaded) {return <AwesomeLoadingIndicator /> }return ( <EyzyTreedata={this.state.treeData} /> ) }}
Node's property
Each node of a tree might has a isBatch property. The tree perceives this property as: node has children and it should be received from the server (see fetchData property). It will show an arrow and an user is able to expand the node. Class loading will be added to the node. And upon completion, the class will be removed and new nodes will be added as children to this node.
Example of component data:
consttreeData= [ { text:'Item 1' }, { text:'Item 2' }, { text:'Item 3' }, { text:'Item 4', isBatch:true } // this node will has a "expandable" arrow]// This method must return a PromisefunctionhandleFetchData(node) {// Note that it is important to return a valid JS object (not `fetch` response)returnfetch(`/api?nodeText=${node.text}`).then(response =>response.json())// or you can use a library for the requestreturnaxios.get('/user', { params: { nodeText:node.text } })}// component example<EyzyTreedata={treeData}fetchData={handleFetchData}/>
Using API
There are several methods that allow you to add new nodes as children asynchronously.