我有一个叫做按钮的道具,它的类型是一个数组。我以这样的方式将它传递给我的组件,当它出现时它应该列出该数组中的所有按钮,如下所示:
render() {
return (
<div>
<MyComp title="Some titme" subtitle="subtitle" buttons={this.getButtonArray()}>
</div>
)
}
getButtonArray() {
let buttonArray=[{
type:"aaa",
text: "Downlaod",
onClick: {this.onCancel.bind(this)}//getting an eslint error saying Fatal: this is a reserved keyword
}]
}
这是我定义的 MyComp:
export default class MyComp extends React.Component {
static defaultProps = {
buttons: [{
text: "Confirm",
type: "secondary",
onClick : function() {console.log("Confirm clicked");}
},
{
text: "cancel",
type: "secondary",
onClick : function() {console.log("cancel clicked");}
}]
}
render() {
return (
<div>
{this.props.buttons && this.props.buttons.map(el => <Button key={el.type} text={el.text} type={el.type} onClick={el.onClick}/>)} //<Button> is another component imported.
</div>
)
}
}
任何想法为什么“这个”在那里是不可接受的,我怎么可能有一个对象数组的onclick?
谢谢
您传递的是函数而不是对象,因此您应该删除 {}, onClick: this.onCancel.bind(this) 就足够了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句