2017-05-19 93 views
0

ReactNative: 我用react-navigation組件。ReactNative:在導航欄的右邊按鈕上,點擊事件不工作,出錯

在右側鍵導航欄上,單擊事件不工作

`_newCustomer() { 
     alert('點擊新建'); 
     } 
    static navigationOptions = { 
     title: '客戶列表', 
     headerRight: (
     <Button title={'添加客戶'} onPress={this._newCustomer.bind(this)}/> 
     ), 
     } 

的錯誤:

undefined is not an object(evaluating 'this._newCustomer') 

回答

0

的問題是因爲你想調用this._newCustomer從靜態函數,並且this未定義,因爲您處於靜態函數。

如果你看看React Navigation Docs,你會發現他們在例子中使用了匿名函數。這樣做,或者調用另一個靜態函數。

0

靜態navigationOptions無法鏈接您的動態「this」變量。通常你需要創建一個自定義的Button組件,並使用這個組件來處理點擊事件。

static navigationOptions = ({ navigation }) => { 
    return { 
    headerRight: (
     <EventButton 
     navigation={navigation} 
     /> 
    ), 
    }; 
}; 

然後

export const EventButton = (props) => { 
let testButton = <TouchableHighlight onPress={() => props.navigation.navigate('CreateNewCustomer',{ name: 'CreateNewCustomer'})}> 
    </TouchableHighlight> 
    return testButton 
} 
+0

對不起,這是不正確 –