Javascript 通过在React Native中触发onPress将值传递给方法
不能在render方法中调用函数,只能将函数作为prop传递。要做到这一点,有两种方法, 1.使用Javascript 通过在React Native中触发onPress将值传递给方法,javascript,reactjs,react-native,Javascript,Reactjs,React Native,不能在render方法中调用函数,只能将函数作为prop传递。要做到这一点,有两种方法, 1.使用.bind, 2.使用箭头功能: onPress={this.resPress(item.delivery_id, item.place_name)} {item.res_name} this.resPress(yourData)}>{item.res_name} 如果您已经在使用fat箭头,则不需要绑定该函数。您只需执行以下操作:onPress={(yourData)=>this.resPres
.bind
,
2.使用箭头功能:
onPress={this.resPress(item.delivery_id, item.place_name)}
{item.res_name}
this.resPress(yourData)}>{item.res_name}
如果您已经在使用fat箭头,则不需要绑定该函数。您只需执行以下操作:onPress={(yourData)=>this.resPress(yourData)}
。
onPress={this.resPress(item.delivery_id, item.place_name)}
<TouchableOpacity onPress={this.resPress.bind(this, yourData)} ><Text>{item.res_name}</Text></TouchableOpacity>
<TouchableOpacity onPress={() => this.resPress(yourData)} ><Text>{item.res_name}</Text></TouchableOpacity>