React Native Tree View for Android and IOS devices

React Native Tree View

This is an example to create a React Native Tree View using react-native-final-tree-view for Android and IOS devices. A Tree View is simply a hierarchical view that is used to show the parent-child relationship between the data. You can simply understand the Tree View using the following image. In the below image, the yellow ball is the parent, and the green ones are the children of the yellow ball. Please remember tree structure has no child limit so green children can also have a child and there can be more children under them.

In this example, we will see how to create a TreeView in React Native. So let’s get started.

We are going to use TreeView component from  react-native-final-tree-view library.

Import TreeView using

import TreeView from 'react-native-final-tree-view';

To Show TreeView

<TreeView
  data={state.data}
  renderNode={({ node, level, isExpanded, hasChildrenNodes }) => {
    return (
      <View>
        <Text
          style={{
            marginLeft: 25 * level,
            fontSize: 18,
          }}>
          {getIndicator(isExpanded, hasChildrenNodes)} {node.name}
        </Text>
      </View>
    );
  }}
/>

For this example, we are going to use a dummy data array to make a screen with 2 parents having multiple child hierarchies under them.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use TreeView we need to install react-native-final-tree-view dependency.

To install this open the terminal and jump into your project using

cd ProjectName

Run the following command to install

npm install react-native-final-tree-view --save

This command will copy all the dependencies into your node_module directory.

–save is optional, it is just to update the react-native-final-tree-view dependency in your package.json file.

Code

Now Open App.js in any code editor and replace the code with the following code

App.js

// React Native Tree View for Android and IOS devices
// https://aboutreact.com/react-native-final-tree-view/

// import React in our code
import React from 'react';

// import all the components we are going to use
import {SafeAreaView, Text, View} from 'react-native';

//import library for the TreeView
import TreeView from 'react-native-final-tree-view';

//Dummy data for the Tree View
const state = {
  data: [
    {
      id: 'Parent1',
      name: 'Parent1',
      children: [
        {
          id: 'child1',
          name: 'child1',
          children: [
            {
              id: 'child11',
              name: 'child11',
              children: [
                {
                  id: 'child111',
                  name: 'child111',
                },
              ],
            },
            {
              id: 'child12',
              name: 'child12',
            },
          ],
        },
      ],
    },
    {
      id: 'Parent2',
      name: 'Parent2',
      children: [
        {
          id: 'child2',
          name: 'child2',
          children: [
            {
              id: 'child21',
              name: 'child21',
            },
            {
              id: 'child22',
              name: 'child22',
            },
          ],
        },
      ],
    },
  ],
};

const getIndicator = (isExpanded, hasChildrenNodes) => {
  if (!hasChildrenNodes) {
    return '*';
  } else if (isExpanded) {
    return '-';
  } else {
    return '+';
  }
};

const App = () => {
  return (
    <SafeAreaView style={{flex: 1}}>
      <View style={{padding: 10}}>
        <TreeView
          data={state.data}
          renderNode={
            ({
              node,
              level,
              isExpanded,
              hasChildrenNodes
            }) => {
            return (
              <View>
                <Text
                  style={{
                    marginLeft: 25 * level,
                    fontSize: 18,
                  }}>
                  {getIndicator(isExpanded, hasChildrenNodes)}
                  {node.name}
                </Text>
              </View>
            );
          }}
        />
      </View>
    </SafeAreaView>
  );
};

export default App;

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Output Screenshots

   

Output in Online Emulator


This is how you can make a React Native Tree View for Android and IOS using react-native-final-tree-view. If you have any doubts or want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

8 thoughts on “React Native Tree View for Android and IOS devices”

  1. I am implimenting TreeView in React Native same like mentioned above, but I am getting error as below:

    Invariant Violation: Element type is invalid: expected a string(for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your vomponent from the file it’s defined in, or you might have mixed up default and named imports.

    Please help me to fix this error.

    Reply
  2. Hi, I want to modify the rendernode UI with 2 textview and Image, is it possible to edit?
    If yes could you please help me how to edit..
    Thanks

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.