Phil
Phil

Reputation: 320

React Recursion

Im trying to let a function call itself to do some recursion. But I cant seem to find out how to access the function from within itself. Probably a syntax problem - can anyone help?. It throws TypeError: Cannot read property 'recursiveRenderChildren' of undefined

import React, { Component } from 'react';

export default class ComposeDom extends Component {

    render() {
        return <div>
            {this.recursiveRenderChildren(this.props.cdom.treeData)}
        </div>
    }
    recursiveRenderChildren(children) {
        return children.map(function(child){
            console.log(child);
            return <child.slug key={child.title}>{child.title} {this.recursiveRenderChildren(child.children)}</child.slug>
        })
}

Upvotes: 0

Views: 1955

Answers (1)

VivekN
VivekN

Reputation: 1602

The function should be like this:-

recursiveRenderChildren(children) {
        let that = this;
        return children.map(function(child){
          console.log(child);
            return <child.slug key={child.title}>{child.title} {that.recursiveRenderChildren(child.children)}</child.slug>
        })
  }

Upvotes: 1

Related Questions