reactjs - 使用 ReactJS/Reactstrap 进行 DropdownMenu 和 DropdownItem onClick

标签 reactjs drop-down-menu onclick reactstrap

我想要一个带有 dropdownItem 的 dropdownMenu,

当我点击一个项目时,它会显示引用项目

enter image description here

但实际上当我点击时,什么也没有发生

切换功能:处理下拉菜单

handleClick:应该打印我的事件引用,但它从未抛出

我做了什么:(我自愿删除一些代码以保留问题的目的,this.props.currentEvents 已填充完毕)

class DefaultHeader extends Component {

  constructor(props) {
    super(props);

    this.toggle = this.toggle.bind(this);
    this.state = {
      dropdownOpen: false,
      selectedEvents: "Choose Your Event",
    };
  }

  toggle() {
    this.setState({
      dropdownOpen: !this.state.dropdownOpen
    });
  }

  handleClick = (eventRef, name) => {
    console.log('toogle event ref', eventRef);
    this.setState({selectedEvents: name, selectedEventsID: eventRef});
}

  render() {
    const { children, ...attributes } = this.props;
    return (
      <React.Fragment>

        <ButtonDropdown className="info d-md-down-none" display="lg" mobile isOpen={this.state.dropdownOpen} toggle={this.toggle} style={{marginRight: 10}}>
        <DropdownToggle caret>
         {this.state.selectedEvents}
        </DropdownToggle>
        {this.props.currentEvents.map((event, index) => 
           <DropdownMenu key={index}>
              <DropdownItem onClick={this.handleClick.bind(this,event.eventRef, event.name)}>{event.name}</DropdownItem>
           </DropdownMenu>
        ): (<DropdownMenu>
                <DropdownItem>You don't manage any event for the moment</DropdownItem>
           </DropdownMenu>)

      </React.Fragment>
    );
  }
}

我已经使用过这样的 onClick 函数,它可以工作,但是对于 dropdownMenu,它不起作用,看起来该函数从未被调用,或者调用了切换而不是 onClickHandle(),

有什么建议吗?

最佳答案

我也遇到了同样的问题。 我的解决方案如下。

              <DropdownItem onClick={this.handleClick.bind(this,event.eventRef, event.name)}>{event.name}</DropdownItem>

相反

              <DropdownItem onClick={() => this.handleClick.bind(this,event.eventRef, event.name)}>{event.name}</DropdownItem>

关于reactjs - 使用 ReactJS/Reactstrap 进行 DropdownMenu 和 DropdownItem onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301744/

相关文章:

javascript - Action 永远不会到达 reducer ?

javascript - 在大型 TypeScript 项目中共享类型的最佳方式?

javascript - React 的 setState() 是否保证每次都创建一个新的状态对象?

javascript - 如何使我的下拉菜单与我的元素正确对齐?

javascript - 表单提交后如何保留下拉选择的值?

javascript - 将提示值传递到输入按钮上的 "click"事件触发的函数中

asp.net - 为什么 ASP.NET MVC a href onclick 会立即触发?

CSS 网格单元格不是正方形

html - 下拉菜单中的样式悬停状态(PayPal)

javascript - 显示 2 个具有相同 ID 和 Class onClick 的 Div