【问题标题】:How to update the button title?如何更新按钮标题?
【发布时间】:2023-03-25 22:04:01
【问题描述】:
<DropdownButton bsStyle="default" title="No caret" noCaret id="dropdown-no-caret"> 
  <MenuItem eventKey="1">Action</MenuItem>
  <MenuItem eventKey="2">Another action</MenuItem>
  <MenuItem eventKey="3">Something else here</MenuItem>
  <MenuItem eventKey="4">Separated link</MenuItem>
</DropdownButton>

我从 react-bootstrap 组件示例中提取了这个示例,https://react-bootstrap.github.io/components.html#btn-dropdowns-nocaret

How would you update the button title when one of the menu item is selected?

【问题讨论】:

    标签: reactjs react-bootstrap


    【解决方案1】:

    使用状态设置标题并向您添加onChange 事件DropdownButton 以更新状态

    <DropdownButton bsStyle="default" title={this.state.btnTitle} noCaret id="dropdown-no-caret" onChange={this.handleChange}> 
      <MenuItem eventKey="1">Action</MenuItem>
      <MenuItem eventKey="2">Another action</MenuItem>
      <MenuItem eventKey="3">Something else here</MenuItem>
      <MenuItem eventKey="4">Separated link</MenuItem>
    </DropdownButton>
    

    功能:

    handleChange = () => {
        var val = 'someValue';
        this.setState({btnTitle: val});  
    }
    

    当您选择任何MenuItem 时,这将更改标题。我希望这个解决方案可以帮助你

    【讨论】:

    • 感谢您的提示。我害怕设置一个 btnTitle 状态,如果另一个 DropdownButton 被更新,它将被更新。我能够得到一些结果,但您的答案已经得到了完美的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多