无法在React中设计下拉菜单

时间:2018-09-10 14:40:45

标签: javascript reactjs

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



class Navigation extends Component {

    state = {
      topCategory: []
    }

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

  render() {

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

    return (

        <DropdownMenu>
        <li key={navList.uniqueID}> <button onClick={this.showMenu}>{navList.name}</button></li>
        </DropdownMenu>
      )

  })

  return (
    <div>
    <ul className="header">{navList}</ul>
    </div>

  )

  }


  }

  export default Navigation;

import React, { Component } from 'react';



class DropdownMenu extends Component {

  constructor() {
    super();

    this.state = {
      showMenu: false,
    };

    this.showMenu = this.showMenu.bind(this);
    this.closeMenu = this.closeMenu.bind(this);
  }

  showMenu(event) {
    event.preventDefault();

    this.setState({ showMenu: true }, () => {
      document.addEventListener('click', this.closeMenu);
    });
  }

  closeMenu() {
    this.setState({ showMenu: false }, () => {
      document.removeEventListener('click', this.closeMenu);
    });
  }
  render() {

    return (
        <div>
        {
          this.state.showMenu
            ? (
              <div className="menu">
                <button> Menu item 1 </button>
                <button> Menu item 2 </button>
                <button> Menu item 3 </button>
              </div>
            )
            : (
              null
            )
        }
      </div>
    );
  }
}

export default DropdownMenu;

我是新来的React,并且已经在react js中创建了一个导航菜单。单击导航项后,将出现一个下拉列表。但就我而言,它不起作用,主要是下拉部分,有人可以指导我吗?我尝试了许多方法,但是似乎没有用。如果有人可以帮忙或至少给我看,我将不胜感激

1 个答案:

答案 0 :(得分:0)

我在您的代码中看到了几个问题。

  1. 在导航组件中,我看到onClick是this.showMenu,但是没有绑定到该组件的函数。
  2. 您可能需要阅读并了解状态和道具的工作方式。

希望下面的代码段有所帮助。

class DropdownMenu extends React.Component {

  constructor() {
    super();

    this.state = {
    };   
  }

  
  render() {

    return (
        <div>
        {
          this.props.showMenu
            ? (
              <div className="menu">
                <button> Menu item 1 </button>
                <button> Menu item 2 </button>
                <button> Menu item 3 </button>
              </div>
            )
            : (
              null
            )
        }
      </div>
    );
  }
}

class Navigation extends React.Component {

    state = {
      topCategory: []
    }

    componentDidMount() {
    this.setState({
      topCategory: [{uniqueID:1000,name:'Nav Item 1'},{uniqueID:1001,name:'Nav Item 2'},{uniqueID:1002,name:'Nav Item 3'}]
    })
    }

  render() {

  const { topCategory } = this.state;
  const navList = topCategory.map(navListItem => {

    return (
            <li key={navListItem.uniqueID}> <button onClick={(e) => (this.setState({selected:e.target.innerText}))}>{navListItem.name}</button>
            <DropdownMenu showMenu={(()=>{
            
            return this.state.selected === navListItem.name})()}/></li>
      )

  })
  return (
    <div>
        <ul className="header">{navList}</ul>
    </div>

  )

  }


  }




ReactDOM.render(<Navigation />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'/>