1

Trying to display a hierarquical tree view with React.js

My render method:

render: function(){
  var self= this;
  return(
    <div className="panel panel-flat content-group-lg">
      <div className='panel-body'>
        <div className="tree">
          <ul>
            <li>
              <a href="#">Categorias</a>                  
                { self.printTree(this.state.tree_array) }                        
            </li>  
          </ul>        
        </div>
      </div>
    </div>
  )
}

and my printTree method:

printTree: function(level){
  console.log(level);
  var self = this;
  var level_length = level.length;

  if(level_length >= 1){
    return(
      <ul>{
        level.map(function(category){
          return (
            <li key={category.fields.name}>
              <a href='#'>{category.fields.name}</a>
            </li>
            {self.printTree(category.sons_array)}
          )         
        })
      }</ul>
    )
  }
}

The printTree method receives an array that corresponds to a "level" of the tree. My goal is to 'print' each element of this level ( so I use the map to iterate over the array ) but also print any possible sublevels of the current element, so I tried to call the function recursively, passing the 'sons' of the current element as the new array parameter for the recursive call.

For some reason, calling:

{self.printTree(category.sons_array)}

gives me: Uncaught SyntaxError: http://127.0.0.1/gims-webapp/components/NavBar.js: Unexpected token (901:84)

I'm using JSX and can't figure out what JSX is complaining about..

EDIT:

My final structure should look like this:

<div class="tree">
    <ul>
        <li>
            <a href="#">Parent</a>
            <ul>
                <li>
                    <a href="#">Child</a>
                    <ul>
                        <li>
                            <a href="#">Grand Child</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#">Child</a>
                    <ul>
                        <li><a href="#">Grand Child</a></li>
                        <li>
                            <a href="#">Grand Child</a>
                            <ul>
                                <li>
                                    <a href="#">Great Grand Child</a>
                                </li>
                                <li>
                                    <a href="#">Great Grand Child</a>
                                </li>
                                <li>
                                    <a href="#">Great Grand Child</a>
                                </li>
                            </ul>
                        </li>
                        <li><a href="#">Grand Child</a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>
3
  • Could you give the full error please? (Instead of "...") Commented Mar 4, 2016 at 13:29
  • @Cohars I posted the relevant parts. The error is not that intuitive, but anyway i'll edit it. Commented Mar 4, 2016 at 13:35
  • i was expecting something else after Unexpected token :) and well, it's frustrating to see incomplete errors Commented Mar 4, 2016 at 13:43

2 Answers 2

2

If you paste your code to babel repl, you will see where is error in your example – it is inside level.map(...), because you should return only one react node in map method (but you have <li > + self.printTree there). You can put self.printTree inside this li:

level.map(function(category){
  return (
    <li key={category.fields.name}>
      <a href='#'>{category.fields.name}</a>
      {self.printTree(category.sons_array)}
    </li>
   );
 })
Sign up to request clarification or add additional context in comments.

2 Comments

@Pavarine but in your example you have exactly the same – li>a+ul, where ul is result of self.printTree
Your right!!! Don't mind about it...the structure is exact the same! I accepted @Cohars answer cause he posted some minutes before you, but i really thank you for the help Alexandr!!
1

JSX requires expects only one Root Node, so this code:

return (
    <li key={category.fields.name}>
        <a href="#">{category.fields.name}</a>
    </li>
    {self.printTree(category.sons_array)}
);

Should be changed to:

return (
    <li key={category.fields.name}>
        <a href="#">{category.fields.name}</a>
        {self.printTree(category.sons_array)}
    </li>
);

Or it's like returning:

<li>
 ...
</li>
<ul>
 ...
</ul>

Which are several root nodes.

3 Comments

I think putting the recursive call inside the <li> would not satisfy my needs of the hierarchy structure. I edited my question with the desired structure. Can it be adapted?
Don't mind about it...your solution is correct..the structure can be build this way you told.. really apreciate your help...
Now if we want to show serial number in front of each row, how to calculate index value in increment manner inside recursive method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.