2

I'm trying to make a tree structure in react. I use redux to hold data. I'am trying render this render tree by method 'renderTree' but I do something wrong

Here is my code:

import React, { Component } from 'react'
import {treeActions} from '../_actions'
import { connect } from 'react-redux';

class TreePage extends Component {

    componentDidMount() {
        this.props.getTree()

    }

    renderTree(parentId) {
        const nodes = this.items && this.items.filter(x => x.parentNodeId == parentId);
        <ul>
            {nodes && nodes.map((item) => {
                <li>{item.name}</li>

                item.subNodes && this.renderTree(item.NodeId)
            })}
        </ul>
    }

    render() {
        const {items} = this.props.tree;
        this.items = items 

        return(
            <React.Fragment>
                <h1>Hello World!</h1>
                {this.renderTree()}
            </React.Fragment>
        )
    }
}
function mapState(state){


    return state;
}
const actionCreators = {
    getTree: treeActions.getTree
}

const connectedApp = connect(mapState, actionCreators)(TreePage)
export {connectedApp as TreePage}

And here is the photo of items: Photo of nodes

And here is the message from console

./src/TreePage/TreePage.js
  Line 14:9:   Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 16:17:  Expected an assignment or function call and instead saw an expression  no-unused-expressions

Search for the keywords to learn more about each error.

What could be the problem?

2
  • Can you post your tree data structure?
    – timotgl
    May 24, 2020 at 11:29
  • I embed above a screen of the structure May 24, 2020 at 11:31

2 Answers 2

3

The rendering can be simplified a bit, since the "root" of the tree in your case is already an array of nodes, and every node always as a subNodes property, which is always an array. Using the parentId can be avoided completely, since we're rendering from the root and always follow the subNodes.

import React, { Component } from 'react';

const tree = [
  {
    name: 'node1',
    subNodes: [
      {
        name: 'node1-1',
        subNodes: [],
      },
      {
        name: 'node1-2',
        subNodes: [],
      },
    ],
  },
  {
    name: 'node2',
    subNodes: [],
  },
  {
    name: 'node3',
    subNodes: [
      {
        name: 'node3-1',
        subNodes: [
          {
            name: 'node3-1-1',
            subNodes: [],
          },
          {
            name: 'node3-1-2',
            subNodes: [],
          },
        ],
      },
      {
        name: 'node3-2',
        subNodes: [],
      }
    ],
  },
];

class Tree extends Component {

  renderSubNodes(subNodes) {
    return (
      <ul>
        {subNodes.map((node) => (
          <li>
              {node.name}
              {this.renderSubNodes(node.subNodes)}
          </li>
        ))}
      </ul>
    );
  }

  render() {
    return(
      <React.Fragment>
        <h1>Hello World!</h1>
        {this.renderSubNodes(tree)}
      </React.Fragment>
    )
  }
}

export default Tree;
2
  • it makes sense, thanks, but now I have a problem to receive this structure on the backend site :( stackoverflow.com/questions/61986397/… May 24, 2020 at 13:07
  • @KrystianWolański Can't help with that but good luck :) One last comment maybe, since you're modelling this data structure: Differentiating between sub nodes and sub leafs is not strictly necessary, you could just treat a leaf as a node that has an empty array for its subNodes property. Traversing the tree is simpler that way. Otherwise, if you're interested in all children of one node, you have to look into two different arrays of nodes.
    – timotgl
    May 25, 2020 at 12:21
1

The issue is that you haven't returned your JSX from within the renderTree function.

 renderTree(parentId) {
        const nodes = this.items && this.items.filter(x => x.parentNodeId == parentId);
        return (<ul>
            {nodes && nodes.map((item) => { 
              return (
                <React.Fragment> 
                   <li>{item.name}</li>
                   {item.subNodes && this.renderTree(item.NodeId)}
                </React.Fragment>
              )
            })}
        </ul>
       )
    }
1
  • Still is the same error but I reworked to renderTree(parentId) { this.items && this.items.filter(x => x.parentNodeId === parentId) .map((item) => { return ( <ul> <li>{item.name}</li> <React.Fragment>{item.subNodes && this.renderTree(item.nodeId)}</React.Fragment> </ul> ) }) } Now there are not error but nothing is renderer May 24, 2020 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.