React Native 使用键值对 props 创建 class
React Native create class with key-value pair props
class Adder extends Component {
constructor(props) {
super(props)
this.props.callback(this.props.valueA + this.props.valueB)
}
}
在 JSX 中我可以这样做:
<Adder callback={this.resultFunc} valueA={4} valueB={2}/>
我不知道 JS 中的语法,例如这行不通。我只得到第一个参数:
this.myAdder = new Adder({callback:this.resultFunc},{valueA:4},{valueB:2});
除了第一个 KV 对之外,在 Adder class 中未定义。
任何人都可以指出我的权利吗?谢谢!
您应该能够将它们全部放在同一个对象中:
this.myAdder = new Adder({callback: this.resultFunc, valueA: 4, valueB: 2});
如你所见,加法器只接受一个参数,但你传递了三个
道具是单个物体
this.myAdder = new Adder({callback:this.resultFunc,valueA:4,valueB:2});
你应该只有一个对象:new Adder({callback:this.resultFunc, valueA:4, valueB:2}) 这将是你的道具。
class Adder extends Component {
constructor(props) {
super(props)
this.props.callback(this.props.valueA + this.props.valueB)
}
}
在 JSX 中我可以这样做:
<Adder callback={this.resultFunc} valueA={4} valueB={2}/>
我不知道 JS 中的语法,例如这行不通。我只得到第一个参数:
this.myAdder = new Adder({callback:this.resultFunc},{valueA:4},{valueB:2});
除了第一个 KV 对之外,在 Adder class 中未定义。 任何人都可以指出我的权利吗?谢谢!
您应该能够将它们全部放在同一个对象中:
this.myAdder = new Adder({callback: this.resultFunc, valueA: 4, valueB: 2});
如你所见,加法器只接受一个参数,但你传递了三个 道具是单个物体
this.myAdder = new Adder({callback:this.resultFunc,valueA:4,valueB:2});
你应该只有一个对象:new Adder({callback:this.resultFunc, valueA:4, valueB:2}) 这将是你的道具。