1

我正在尝试使用 ref='roleType'获取DropdownButton选择的 get 值,但无论我尝试什么都失败了。

    <DropdownButton ref='roleType' bsStyle='link' title='Role' key='1'  bsSize='xsmall'>
      {_items}
    </DropdownButton>

我尝试了以下方法:

 React.findDOMNode(this.refs.roleType)
 this.refs.roleType.getDOMNode()

注意:我不确定它是否重要。a在DropdownButtona 内Panel,它在 asection的 a内div

4

1 回答 1

2

文档中所述,您应该使用 的onSelect道具。DropdownButton

var DropdownButton = ReactBootstrap.DropdownButton;
var MenuItem = ReactBootstrap.MenuItem;

var Hello = React.createClass ({
    getInitialState() { 
        return { key: null }
    },

    onSelect(key) {
        this.setState({ key: key });
    },

    render() {
        var selected = this.state.key ? <p>Selected: {this.state.key}</p> : '';
        return (<div>
        <DropdownButton bsStyle="primary" title="Test" onSelect={this.onSelect}>
          <MenuItem eventKey='1' active={this.state.key==='1'}>Action</MenuItem>
          <MenuItem eventKey='2' active={this.state.key==='2'}>Another action</MenuItem>
          <MenuItem eventKey='3' active={this.state.key==='3'}>Active Item</MenuItem>
          <MenuItem divider />
          <MenuItem eventKey='4' active={this.state.key==='4'}>Separated link</MenuItem>
        </DropdownButton>
        {selected}
        </div>);
    }
});

React.render(<Hello/>, document.getElementById('container'));

这是一个工作小提琴:

https://jsfiddle.net/gadr/azm159g4/2/

于 2015-07-08T12:19:13.157 回答