Async content

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'

export default 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 (
            <EyzyTree data={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:

Using API

There are several methods that allow you to add new nodes as children asynchronously.

  • append (see API)

  • prepend (see API)

I see no reason to re-describe how the methods work (you can look at the link above). Better I will show a simple and real example.

Insertion options

There are few options:

  • expand whether to expand node

  • loading whether to show loading indicator

  • index insertion index

Last updated