简体   繁体   English

为什么渲染未返回任何内容?

[英]Why was nothing returned from render?

I am trying to generate my navigation drawer from JSON and have got everything working using just components but I am refactoring to functions where I can for performance (and to learn more about React and Javascript). 我试图从JSON生成我的导航抽屉,并且仅使用组件就可以使所有工作正常进行,但是我正在重构为可以提高性能的功能(并更多地了解React和Javascript)。 If I leave DrawerLink as a seperate component then everything works as expected, but if I try to pass renderLink down from PopulateDrawer (parent component) it breaks and throws the error that nothing was returned from render. 如果我将DrawerLink保留为单独的组件,那么一切都会按预期工作,但是如果我尝试从PopulateDrawer(父组件)向下传递renderLink,它会中断并抛出错误,即渲染未返回任何内容。

As far as I can tell I am following the instructions from here https://reactjs.org/docs/handling-events.html and here https://material-ui.com/demos/buttons/ correctly but it doesn't work. 据我所知,我正确地遵循了https://reactjs.org/docs/handling-events.htmlhttps://material-ui.com/demos/buttons/上的指示,但是没有工作。 Is it because I am passing through a component rather than a function? 是因为我正在通过组件而不是函数吗? Is this where Higher Order Components come into play? 这是高阶组件起作用的地方吗? I've seen them mentioned but haven't understood how to use them properly yet. 我看过他们提到的内容,但还不了解如何正确使用它们。

import React from 'react';
import Divider from '@material-ui/core/Divider';
import ExpandLess from '@material-ui/icons/ExpandLess';
import ExpandMore from '@material-ui/icons/ExpandMore';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import { Link } from 'react-router-dom';
import { List, Collapse } from '@material-ui/core';

class DrawerLink extends React.Component {
  renderLink = itemProps => <Link to={this.props.link} {...itemProps} />;

  render() {
    const { display } = this.props;

    return (
        <ListItem button component={this.renderLink}>
            <ListItemText primary={display} />
        </ListItem>
    );
  }
}

function ProcessJSON(props) {
  const {
    data, 
    groupDrawers,
    updateGroupDrawer,
    renderLink
  } = props;

  return (
    Object.keys(data).map(key => {
      if (data[key] === "divider") {
        return (
          <Divider key={key} />
        )
      }
      else {
        let childData = data[key];
        let listDetected = false;

        Object.keys(childData).map(key => {
          if (key == "list") {
            listDetected = true;
          }
        })

        if (listDetected === false) {
          return (
            // <DrawerLink
            //   key={childData.display}
            //   display={childData.display} 
            //   link={childData.link}
            //   renderLink={renderLink} />

            <ListItem key={childData.display} button component={(e) => renderLink(childData.link)}>
              <ListItemText primary={childData.display} />
            </ListItem>
          )
        }
        else {
          return (
            <React.Fragment key={childData.display}>
              <ListItem button onClick={() => updateGroupDrawer(childData.display, !groupDrawers[childData.display])}>
                <ListItemText primary={childData.display} />
                {groupDrawers[childData.display] ? <ExpandLess /> : <ExpandMore />}
              </ListItem>
              <Collapse 
                in={groupDrawers[childData.display]} 
                timeout="auto" unmountOnExit >
                <List component="div">
                  <ProcessJSON 
                    data={childData.list} 
                    groupDrawers={groupDrawers} 
                    updateGroupDrawer={updateGroupDrawer} />
                </List>
              </Collapse>
            </React.Fragment>
          )
        }
      }
    })
  )
}

class PopulateDrawer extends React.Component {
  componentDidMount() {
    const groupDrawers = {...this.state.groupDrawers, ...this.props.groupDrawers};
    this.setState({ groupDrawers });
  };

  state = {
    groupDrawers: {}
  };

  updateGroupDrawer = (display, open) => {
    const groupDrawers = {...this.state.groupDrawers};
    groupDrawers[display] = open;
    this.setState({ groupDrawers });
  };

  renderLink = (link) => {
    <Link to={link} />
  };

  render() {
    const {
      data,
    } = this.props;

    return (
      <ProcessJSON 
        data={data} 
        groupDrawers={this.state.groupDrawers} 
        updateGroupDrawer={this.updateGroupDrawer} 
        renderLink={this.renderLink}/>
    )
  }
}

export default PopulateDrawer;

edit, adding JSON blob code is running against: 编辑,添加JSON Blob代码正在针对:

"data": {
        "nav1": {
          "display": "Home",
          "link": "/"
        },
        "nav2": {
          "display": "Templates",
          "link": "templates"
        },
        "nav3": "divider",
        "nav4": {
          "display": "Test",
          "list": {
            "nav1": {
              "display": "Home",
              "link": "/"
            },
            "nav2": {
              "display": "Templates",
              "link": "templates"
            }
          }

Are you sure display is neither null or undefined ? 您确定display内容不是null还是undefined It looks as though display is a property off of childData . 看起来displaychildData一个属性。 Try logging display . 尝试记录display

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM