react ice tree报错type

线上的tree报了错,connot read ptoperty 'type' of undefined

后面发现 这里有问题,没有判断条件不满足时返回为空,所以会自己返回undefined,导致报错

const loop = (data) => data.map(item => {
        if (item.branchType == 1 || item.branchType == 2 || item.branchType == 3) {
          return <TreeNode
            label={<Fragment>{Icon(item.branchType)}{item.branchSimpleName}</Fragment>}
            key={item.id}
            data={item}
            >
            {item.children && item.children.length > 0 ? loop(item.children) : null}
            </TreeNode>
        } else {
      // 此处加一个返回为空的判断
return '' } })
{ data && data.length > 0 ? <React.Fragment>
            <Tree
              defaultExpandAll={false}
              autoExpandParent={autoExpandParent}
              draggable={true}
              defaultExpandedKeys={defaultExpandedKeys}
              filterTreeNode={filterTreeNode}
              expandedKeys={expandedKeys}
              onExpand={this.handleExpand}
              onSelect={this.treeNodeonRightClick}
              animation={true}
              selectedKeys={selectedKeys}
              showLine
              >
              {loop([...data])}
            </Tree>
          </React.Fragment> : ''}
原文地址:https://www.cnblogs.com/zhuangcui/p/12937816.html