2016-09-14 130 views
0
var Restaurant = React.createClass({ 
resPress: function (resId, resName) { 
    this.props.navigator.push({ 
     name: 'viewMenu', 
     id: resId, 
     placeName: resName 
    }); 
}, 
render: function() { 
    var that = this; 
    return (
      <View> 
       {this.state.place.map((item, i) => (<TouchableOpacity key={i} onPress={() => this.resPress(item.res_id, item.res_name)} ><Text>{item.res_name}</Text></TouchableOpacity>))} 
      </View> 
    ) 
} 
}); 

這很好。我的問題是爲什麼我不能像下面提到的那樣將值傳遞給'resPress'?通過在React Native中觸發onPress將值傳遞給方法

onPress={this.resPress(item.delivery_id, item.place_name)} 

回答

3

請看這link。 因爲你可以通過一個函數來onPress,你不能執行一個功能them.for傳遞值內像這樣(onPress={this.resPress(item.delivery_id, item.place_name)})語法,你必須這樣做:

<TouchableOpacity onPress={this.resPress.bind(this,yourData)} ><Text>{item.res_name}</Text></TouchableOpacity> 
+2

你不需要'bind'該功能,如果你已經在使用胖箭頭了。你可以簡單地做:'onPress = {(yourData)=> this.resPress(yourData)}'。 –

+0

是啊,我對Apologize.tnx發表評論。 –

相關問題