反应递归

时间:2017-06-05 17:44:48

标签: reactjs recursion

我试图让函数调用自己做一些递归。但我似乎无法找到如何从内部访问该功能。可能是语法问题 - 任何人都可以帮忙吗?它抛出 TypeError:无法读取属性&requoiveRenderChildren'未定义的

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>
        })
}

1 个答案:

答案 0 :(得分:1)

该功能应如下所示: -

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>
        })
  }