var monthSelections = {
"01": "January",
"02": "February",
"03":"March",
"04":"April",
"05": "May",
"06":"June",
"07":"July",
"08":"August",
"09":"September",
"10": "October",
"11":"November",
"12": "December",
"": "full year"
};
和代码:
getInitialState(){
return {
DropdownSelected: monthSelections["04"]
}
},
handleDropDown(x){
this.setState({DropdownSelected:x });
}
DropDown = React.createClass({
render() {
return(
<div className="dropdown ">
<button className="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
{this.state.DropdownSelected}
<span className="caret"></span>
</button>
<ul className="dropdown-menu" aria-labelledby="dropdownMenu1" value={this.state.DropdownSelected}>
{Object.keys(monthSelections).map(function(month){
return (
<li ><a href="#" onClick={this.handleDropDown(monthSelections[month]) }> {monthSelections[month]} </a></li>
);
}.bind(this))
}
</ul>
</div>
);
}
});
答案 0 :(得分:1)
我想问题就在这里:onClick={this.handleDropDown(monthSelections[month])
你不能以这种方式给回调提供参数。你可以试试这个:
onClick={this.handleDropDown.bind(null, monthSelections[month])}
。