我想通過按鈕單擊將數據傳遞到另一個屏幕。我正在使用路由器通量,它工作得很好,但在反應導航它不會工作。 因此,在路由器通量,在按一下按鈕,它會調用這個函數:反應本地通過數據與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>
你做正確的事,但是如果你傳入catch中,你的ajax請求出錯。 – WilomGfx
你是什麼意思?通過路由器通量,我只能得到「0輛汽車!」如果沒有數據,但是在反應導航中,即使有數據,我也會得到該警報,並且不會打開下一個屏幕。 @WilomGfx – waze
爲什麼不嘗試console.log(錯誤)而不是警報0汽車。所以你知道錯誤是什麼。 – cjmling