我有关于在回调函数传递参数的问题。我使用redux-form
,当我改变SkinList
中的选择时,它会触发onChange
回调- activeSkinChange
方法
activeSkinChange
中,调用来自SkinList handlSkinChange属性的handlSkinChange
。我需要将选择值传递给handlSkinChange (event.target.value)
export default class SkinList extends Component {
activeSkinChange = (event) => {
this.props.handlSkinChange(event.target.value);
}
render() {
const { skinList, activeSkin } = this.props;
return (
<div>
<select className="form-control" onChange={this.activeSkinChange} value={activeSkin}>
{this.renderOptions(skinList)}
</select>
</div>
);
}
}
然后移出SkinList:
export default class Control extends Component {
onHandlSkinChange = (?, ?) => {
};
render() {
return (
<Field
name="skin.colors.activeSkin"
component={activeSkin =>
<SkinList skinList={skinList} activeSkin={activeSkin} handlSkinChange={this.onHandlSkinChange(activeSkin)}/>
}/>
);
}
}
handlSkinChange
将触发回调this.onHandlSkinChange
,我还需要在SkinList props的this.onHandlSkinChange()
中传递另一个参数activeSkin
,但它会遇到错误…
意味着在onHandlSkinChange
方法中需要传递两个参数,一个是选择值(event.target.value),另一个是activeSkin我该怎么做,让我在回调函数中传递参数在连续回调函数?任何想法?
export default class SkinList extends Component {
activeSkinChange (event){
this.props.handlSkinChange(event.target.value);
}
render() {
const { skinList, activeSkin } = this.props;
return (
<div>
<select className="form-control" onChange={this.activeSkinChange.bind(this)} value={activeSkin}>
{this.renderOptions(skinList)}
</select>
</div>
);
}
}
export default class Control extends Component {
onHandlSkinChange (value, activeSkin ) {
console.log("CHECK",value,activeSkin);
}
render() {
return (
<Field
name="skin.colors.activeSkin"
component={activeSkin => <SkinList skinList={skinList} activeSkin={activeSkin}
handlSkinChange={v=>this.onHandlSkinChange(v,activeSkin)}/>
}/>
);
}
}