React Bootstrap 下拉按钮 OnSelect
React Bootstrap Dropdown Button OnSelect
我将选项值传递给一系列下拉按钮,每个按钮都位于数据数组的子组件中。
当在其中一个按钮中选择一个选项时,我将使用 onSelect 的结果更新父组件中的状态。这一切都很好...
//parent component
sourceSelected = (event) => {
this.setState({
sourceSelected: event
});
...
<ButtonToolbar>
{MEDIUM.map((medium) =>
<Medium key={medium.medium_name} medium={medium} onSelectedValue{this.sourceSelected } />
)};
</ButtonToolbar>
//child component
<DropdownButton title={props.medium.medium_name} id="source-dropdown" onSelect={props.onSelectedValue}>
{props.medium.source.map((option, index) =>
<MenuItem key={index} eventKey={option}> {option} </MenuItem>)}
</DropdownButton>
但是,我还想在状态 (mediumSelected=???) 中存储选择该选项的按钮的名称。
有没有办法让 OnSelect 将其传回,或者我应该做其他事情吗?
您可以利用 Javascript 事件和 this
。基本上,将事件传递给将使用按钮名称的函数,就像这样
<button name="btn" onClick={e => this.buttonName(e.target.name)}>
您还需要在 constructor()
中绑定 this
示例代码:
constructor(props) {
super(props);
// Bind this so you can use it below
this.buttonName = this.buttonName.bind(this);
}
buttonName(e) {
console.log(e);
}
render() {
return (
<div>
// Pass the name of the button to the function
<button name="btn" onClick={e => this.buttonName(e.target.name)}>
Button
</button>
</div>
);
}
我还在 https://codesandbox.io/s/lrwqr303vz 上举了一个简单的例子。不要介意文件名。
好的,我用...回答了这个问题 https://reactjs.org/docs/handling-events.html 将参数传递给事件处理程序。
密码是:
//parent component
sourceSelected = ( medium_name, event) => {
this.setState({
sourceSelected: event,
mediumSelected: medium_name
});
}
...
<div className='test'>
<ButtonToolbar>
{MEDIUM.map((medium) =>
<Medium key={medium.medium_name} medium={medium} onSelectedValue={this.sourceSelected.bind(this, medium.medium_name) } />
)};
</ButtonToolbar>
我将选项值传递给一系列下拉按钮,每个按钮都位于数据数组的子组件中。
当在其中一个按钮中选择一个选项时,我将使用 onSelect 的结果更新父组件中的状态。这一切都很好...
//parent component
sourceSelected = (event) => {
this.setState({
sourceSelected: event
});
...
<ButtonToolbar>
{MEDIUM.map((medium) =>
<Medium key={medium.medium_name} medium={medium} onSelectedValue{this.sourceSelected } />
)};
</ButtonToolbar>
//child component
<DropdownButton title={props.medium.medium_name} id="source-dropdown" onSelect={props.onSelectedValue}>
{props.medium.source.map((option, index) =>
<MenuItem key={index} eventKey={option}> {option} </MenuItem>)}
</DropdownButton>
但是,我还想在状态 (mediumSelected=???) 中存储选择该选项的按钮的名称。
有没有办法让 OnSelect 将其传回,或者我应该做其他事情吗?
您可以利用 Javascript 事件和 this
。基本上,将事件传递给将使用按钮名称的函数,就像这样
<button name="btn" onClick={e => this.buttonName(e.target.name)}>
您还需要在 constructor()
this
示例代码:
constructor(props) {
super(props);
// Bind this so you can use it below
this.buttonName = this.buttonName.bind(this);
}
buttonName(e) {
console.log(e);
}
render() {
return (
<div>
// Pass the name of the button to the function
<button name="btn" onClick={e => this.buttonName(e.target.name)}>
Button
</button>
</div>
);
}
我还在 https://codesandbox.io/s/lrwqr303vz 上举了一个简单的例子。不要介意文件名。
好的,我用...回答了这个问题 https://reactjs.org/docs/handling-events.html 将参数传递给事件处理程序。
密码是:
//parent component
sourceSelected = ( medium_name, event) => {
this.setState({
sourceSelected: event,
mediumSelected: medium_name
});
}
...
<div className='test'>
<ButtonToolbar>
{MEDIUM.map((medium) =>
<Medium key={medium.medium_name} medium={medium} onSelectedValue={this.sourceSelected.bind(this, medium.medium_name) } />
)};
</ButtonToolbar>