[英]React Native pass data with react-navigation
我想在點擊按鈕時將數據傳遞到另一個屏幕。 我正在使用router-flux,它運行得很好,但是在反應導航上它不起作用。 因此,在router-flux上,點擊按鈕時,它會調用此函數:
onSearch() {
fetch(`URL`, {
method: 'GET',
})
.then((response) => { return response.json() } )
.then((responseJson) => {
Action.results({data: responseJson});
})
.catch((error) => {
Alert.alert("0 Cars!");
});
}
但是在react-navigation上,如果我將代碼更改為:
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汽車!”。 我究竟做錯了什么? 在router-flux上它非常簡單。
這就是我調用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>
嘗試將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!");
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.