简体   繁体   English

React Bootstrap-DropdownButton不起作用

[英]React Bootstrap - DropdownButton not working

I started learning ReactJS a few weeks ago and I'm struggling with react bootstrap dropdown button. 几周前我开始学习ReactJS,我在挣扎着React Bootstrap下拉按钮。

Here's my code (simplified): 这是我的代码(简体):

import React from "react";
import ReactDOM from "react-dom";
import { DropdownButton, MenuItem } from "react-bootstrap"
import "./bootstrap.min.css";
import "./bootstrap.min.js";

export const GameBoard = props => {
  return (
    <div className="gameBoard container-fluid">
      <div className="row">
        <div className="title text-center">
          <h5>Déroulement de la partie</h5>
          <hr />
        </div>
        <div className="menu">
          <DropdownButton bsStyle="primary" title="Toto" id="toto_0" key="0">
            <MenuItem eventKey="1">Action</MenuItem>
            <MenuItem eventKey="2">Another action</MenuItem>
            <MenuItem eventKey="3" active>Active Item</MenuItem>
            <MenuItem divider />
            <MenuItem eventKey="4">Separated link</MenuItem>
          </DropdownButton>
        </div>
      </div>
    </div>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<GameBoard />, rootElement);

I pasted the demo code found here: https://react-bootstrap.github.io/components/dropdowns/ 我粘贴了在这里找到的演示代码: https : //react-bootstrap.github.io/components/dropdowns/

My button is displayed but nothing happens, I'm using codesandbox to develop. 我的按钮显示出来,但是什么也没有发生,我正在使用codeandbox进行开发。

Thanks 谢谢

I am not familiar with the react-bootstrap package. 我不熟悉react-bootstrap软件包。 But it looks like you need a toggle function. 但看起来您需要一个切换功能。 How does it know to open the dropdown? 如何知道打开下拉菜单? I believe your dropdown component should have a isOpen or open prop and then set that prop to a state variable that changes in a toggle method. 我相信您的下拉组件应该具有isOpen或open属性,然后将该属性设置为在toggle方法中更改的状态变量。

Pseudo Code 伪码

state: {
  isOpen: false
} 

toggle = () => {
  this.setState({ isOpen: !this.state.isOpen });
}

render() {
 const { isOpen } = this.state; 
  return ( 
    <Dropdown open={isOpen} toggle={this.toggle}>
      ...etc.

Perhaps someone with more insight on this package can provide more info. 也许对此软件包有更深入了解的人可以提供更多信息。 Hopefully this puts you in the right direction?... good luck :) 希望这能把您带向正确的方向吗?...祝您好运:)

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

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