kir*_*imi 2 javascript react-native
是否可以functions(not arrow function)使用 button调用多个onPress?
我知道我可以像这样调用多个箭头函数:
onPress={() => { this.functionOne(); this.functionTwo();}}
Run Code Online (Sandbox Code Playgroud)
但是如果我想在没有 的情况下调用多个函数arrow function怎么办?我试过了,但这行不通。
onPress={this.functionOne, this.functionTwo}
Run Code Online (Sandbox Code Playgroud)
最好的方法是使用一个函数来调用您的所有函数,
func = () => {
this.functionOne();
this.functionTwo();
}
onPress={this.func}
Run Code Online (Sandbox Code Playgroud)
或者您可以传递函数数组并在子组件中调用它们,
家长:
onPress=[this.functionOne, this.functionTwo]
Run Code Online (Sandbox Code Playgroud)
孩子:
this.props.onPress.map(func=>{
func()
})
Run Code Online (Sandbox Code Playgroud)