[英]How to do conditional navigation in react native?
您好,我試圖在react native中進行條件導航。在我的項目中,由於api提取請求,我列出了一組課程和類別,因此在單擊每個課程/類別后,它將導航到不同的屏幕。所以我想要的是在第一個屏幕上單擊courses
,我將獲得當前所選項目的type
,無論是course
還是category
。 因此,如果選中項目是因為我想導航到UnitListing
屏幕,如果是category
我想導航到ChapterListing
屏幕。怎么辦?
更新
navigatetoPage = (category) =>{
const page = category.type == "course" ? "UnitListing" : "ChapterListing";
this.props.navigation.navigate(page, {
id: category.id,
type: category.type
})
}
render(){
return(
<View style={st.main_outer}>
{this.state.cats.map(category =>
<TouchableOpacity
style={st.btn} onPress={() => this.navigatetoPage({category})}>
<Text style={{ color: "#fff" }}>View Course</Text>
</TouchableOpacity>
)}
</View>
);
}
如何在此處進行條件導航? 請幫助我找到解決方案。
您可以像這樣檢查類型並瀏覽,也可以在jsx中進行。 但這更干凈
navigatetoPage(data){ const page = data.type == "course" ? "UnitListing" : "ChapterListing"; this.props.navigation.navigate(page, { id: data.id, type: data.type }) } <TouchableOpacity style={st.btn} onPress={() => this.navigatetoPage(data)}> <Text style={{ color: "#fff" }}>View Course</Text> </TouchableOpacity>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.