简体   繁体   English

React Js中的渲染子菜单组件中的问题

[英]Issue in Rendering Sub-menu component in React Js

topNavigation.js topNavigation.js

import React, { Component } from 'react';
import axios from 'axios';
import SubMenu from './subMenu';



class Navigation extends Component {

  state = {
    mainCategory: []
  }

  componentDidMount() {
    axios.get('http://localhost:3030/topCategory')
      .then(res => {
        console.log(res.data.express);
        this.setState({
          mainCategory: res.data.express.catalogGroupView
        })
      })
  }



  render() {

    const { mainCategory } = this.state;
    return mainCategory.map(navList => {
      return (

        <ul className="header">
          <li key={navList.uniqueID}> <button className="dropbtn ">{navList.name}</button>
          <SubMenu below={this.props.navList.catalogGroupView}/>

          </li>
        </ul>

      )

    })

  }


}

export default Navigation;

I'm new to react and trying to make an ecommerce website. 我是新来的反应者,正试图建立一个电子商务网站。 I have designed the homepage. 我设计了主页。 For the navigation, I have used an external api http://149.129.128.3:3737/search/resources/store/1/categoryview/@top?depthAndLimit=-1,-1,-1,-1 对于导航,我使用了外部api http://149.129.128.3:3737/search/resources/store/1/categoryview/@top?depthAndLimit=-1、-1、-1、-1

and mapped the response. 并映射响应。

If I use the below code in place of SubMenu component it works 如果我使用下面的代码代替SubMenu组件,它将起作用

<ul>
                {console.log(navList.catalogGroupView)}
                {
                  navList.catalogGroupView.map(sub=> {
                    return <li key={sub.uniqueID}> <button>{sub.name}</button></li>
                    })
                }
              </ul>

But as per the url endpoint response there are more sub categories which I'm unable to map. 但是根据url端点响应,有更多子类别我无法映射。

I thought of creating a separate component to display the sub menu items. 我想到创建一个单独的组件来显示子菜单项。 But whenever I use this code it doesn't work. 但是,每当我使用此代码时,它都不会起作用。

Submenu.js component Submenu.js组件

import React, { Component } from 'react';


class SubMenu extends Component {


    constructor(props) {
        super(props);
        this.state = {
            subCategory: []
        };
    }

    render() {


        return subCategory.map(sub => {

            return (
                <ul>
                    <li key={sub.uniqueID}> <button>{sub.name}</button></li>
                </ul>

            )
        })

    }

}

export default SubMenu;

Can somebody please help me on this. 有人可以帮我这个忙吗? I would be grateful. 我会很感激。 I'm not getting where I got wrong. 我没有弄错地方。

在此处输入图片说明

You're trying to access subCategory of state. 您正在尝试访问状态子类别。 Here is the working part: 这是工作部分:

import React, { Component } from 'react';


class SubMenu extends Component {


constructor(props) {
    super(props);
    this.state = {
        subCategory: []
    };
}

render() {
    const { subCategory } = this.state; // you should've add this part

    return subCategory.map(sub => {

        return (
            <ul>
                <li key={sub.uniqueID}> <button>{sub.name}</button></li>
            </ul>

        )
    })

}

}

export default SubMenu;

UPDATE Over here: 在这里更新

<SubMenu below={this.props.navList.catalogGroupView}/>

You're trying to access prop, which doesn't exist, so you should do it that way: 您正在尝试访问不存在的prop,因此应采用以下方式:

<SubMenu below={navList.catalogGroupView}/>

And then In Submenu, you should map over the props, not state, because your state always will be empty array(because you're not setting it). 然后在子菜单中,应该映射道具,而不是状态,因为状态始终为空数组(因为未设置状态)。 And you don't need to use state in that case, because you just need to iterate over array. 在那种情况下,您不需要使用状态,因为您只需要遍历数组。 So here is the code of Submenu: 所以这是子菜单的代码:

import React, { Component } from 'react';


class SubMenu extends Component {
    render() {
        const {below} = this.props;
        return below.map(sub => {

            return (
                <ul>
                    <li key={sub.uniqueID}> <button>{sub.name}</button></li>
                </ul>

            )
        })

    }

}

export default SubMenu;

And theoretically working code of topNavigation.js: 以及topNavigation.js的理论上的工作代码:

import React, { Component } from 'react';
import axios from 'axios';
import SubMenu from './subMenu';



class Navigation extends Component {

  state = {
    mainCategory: []
  }

  componentDidMount() {
    axios.get('http://localhost:3030/topCategory')
      .then(res => {
        console.log(res.data.express);
        this.setState({
          mainCategory: res.data.express.catalogGroupView
        })
      })
  }



  render() {

    const { mainCategory } = this.state;
    return mainCategory.map(navList => {
      return (

        <ul className="header">
          <li key={navList.uniqueID}> <button className="dropbtn ">{navList.name}</button>
          <SubMenu below={navList.catalogGroupView}/> 

          </li>
        </ul>

      )

    })

  }


}

export default Navigation;

Hope this helps. 希望这可以帮助。

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

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