我有兩個螢屏,EditProfile和Profile。我試圖將編輯過的名字和日期從EditProfile傳遞給Profile。它們都堆積在ProfileNavigator中,Profile是出現的第一個螢屏,在那里,用戶可以點擊編輯組態檔按鈕并導航到編輯組態檔螢屏。我怎樣才能將更新的編輯螢屏名稱和日期引數傳遞給主組態檔?
const EditProfileScreen = () => {
const navigation = useNavigation();
}
回傳 (
..
<TouchableOpacity style={styles.commandButton} onPress= {() => {navigation.navigate("Profile", {params: {date: date, name: name }})}。}
activeOpacity={0.5}>
<文本 style={styles.panelButtonTitle}>提交</文本>
</TouchableOpacity>
);
而這是簡介螢屏的代碼:
const ProfileScreen = ({route, navigation}) => {
const { date } = route.params; { = route.params;
const { name } = route.params; const { name } = route.params;
回傳 (
..
<Text>{JSON.stringify(date)}</Text>
<Text>{JSON.stringify(name)}</Text>
)
uj5u.com熱心網友回復:
你的方法沒有任何問題,但是你缺少對個人資料螢屏的初始渲染的空處理。
這樣想吧,當你第一次導航到個人資料螢屏時,不會有 route.params,因為你沒有傳遞任何引數,這將導致一個錯誤。
但是當你移動到編輯螢屏并帶著引數回來時,你將會有 route.params,你可以毫無問題地訪問。
所以要像這樣修改你的代碼
<Text>{JSON.stringify(route.params?.date)}</Text>
<Text>{JSON.stringify(route.params?.name)}</Text>
或者,如果你在簡介螢屏的狀態下有簡介物件,根據route.params.
的變化使用useEffect鉤子來更新它。轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326333.html
標籤:
上一篇:安裝NPM導航依賴包時出錯
