2017-04-18 40 views
1

我想通過按鈕單擊將數據傳遞到另一個屏幕。我正在使用路由器通量,它工作得很好,但在反應導航它不會工作。 因此,在路由器通量,在按一下按鈕,它會調用這個函數:反應本地通過數據與react-navigation

onSearch() { 
    fetch(`URL`, { 
     method: 'GET', 
     }) 
     .then((response) => { return response.json() }) 
     .then((responseJson) => { 
     Action.results({data: responseJson}); 
     }) 
     .catch((error) => { 
     Alert.alert("0 Cars!"); 
     }); 
    } 

但在反應的導航,如果我改變的代碼如下:

onSearch() { 
    fetch(`URL`, { 
     method: 'GET', 
     }) 
     .then((response) => { return response.json() }) 
     .then((responseJson) => { 
     this.props.navigation.navigate('Resultados', { data: responseJson }); 
     }) 
     .catch((error) => { 
     Alert.alert("0 Cars!"); 
     }); 
    } 

將無法​​正常工作,我總是會收到警報「0 Cars!」。 我在做什麼錯?在路由器通量上,它非常簡單。

這是我怎麼稱呼onSearch功能:

<Button onPress={this.onSearch} style={{backgroundColor: '#f48529', width: 140, borderRadius: 30, height: 40}} > 
     <Text style={{color: 'white', fontFamily: 'rubik-light', fontSize: 17}}>Search</Text> 
     <Icon size={15} name={'search'} color={'white'} /> 
    </Button> 
+0

你做正確的事,但是如果你傳入catch中,你的ajax請求出錯。 – WilomGfx

+0

你是什麼意思?通過路由器通量,我只能得到「0輛汽車!」如果沒有數據,但是在反應導航中,即使有數據,我也會得到該警報,並且不會打開下一個屏幕。 @WilomGfx – waze

+0

爲什麼不嘗試console.log(錯誤)而不是警報0汽車。所以你知道錯誤是什麼。 – cjmling

回答

0

試着改變你的onPress={this.onSearch}onPress={this.onSearch.bind(this)}

而且還

嘗試

onSearch() { 

    const { navigate } = this.props.navigation; 

    fetch(`URL`, { 
     method: 'GET', 
     }) 
     .then((response) => { return response.json() }) 
     .then((responseJson) => { 
     navigate('Resultados', { data: responseJson }); 
     }) 
     .catch((error) => { 
     Alert.alert("0 Cars!"); 
     }); 
    } 
+0

我得到這個錯誤:無法讀取undefined的屬性導航 – waze

+0

@waze你是否在我的答案中編輯了上面的代碼? 1。 const {navigate} line .. 2nd。導航(然後在語句中的行嗎?那個錯誤是在哪一行? – cjmling

+0

是的,我做了..但我也嘗試過沒有這些變化,錯誤是一樣的。 – waze