接下来要在页面上按下按钮跳页
以及按了左边 header icon 回上一页
正所谓有去有回才不会不知道怎麽办!
const Home = ({navigation}) => {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>Home Screen</Text>
// 加入按钮
<TouchableOpacity
onPress={() => {
navigation.navigate('IconScreen', { title: "icon页" });
}}
style={{backgroundColor: '#aaa', padding: 5, margin: 5}}
activeOpacity={0.6}>
<Text>来去别页!</Text>
</TouchableOpacity>
// End
</View>
);
};
PS. 切记文字都需要放在 Text 内唷!
整个 App.js
import 'react-native-gesture-handler';
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import {
Text,
View,
Image,
Platform,
StyleSheet,
TouchableOpacity,
} from 'react-native';
import IconScreen from '@screens/IconScreen';
import {assets} from '@src/constants';
const Stack = createStackNavigator();
const headerOptions = ({route, navigation}) => ({
title: route.params ? route.params.title : '',
headerTintColor: 'black', // 字体颜色
headerTitleStyle: {alignSelf: 'center', fontSize: 16}, // header 样式
headerStyle: {
height: Platform.OS === 'ios' ? 88 : 44,
}, // 使用装置来判断 header 的高度
headerRight: () => (
<Image source={assets.actionPrimary} style={[styles.actionPrimary]} />
), // 右边放入 icon
headerLeft: () => (
<TouchableOpacity
onPress={() => {
navigation.goBack();
}}>
<Image
source={assets.actionPrimary}
style={[styles.actionPrimary]}
onPress={() => {
navigation.goBack();
}}
/>
</TouchableOpacity>
// 左边放入icon 并使用 navigation.goBack() 及 backToHome() 回上一页
),
});
const Home = ({navigation}) => {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>Home Screen</Text>
<TouchableOpacity
onPress={() => {
navigation.navigate('IconScreen', {title: 'icon页'});
}}
style={{backgroundColor: '#aaa', padding: 5, margin: 5}}
activeOpacity={0.6}>
<Text>来去别页!</Text>
</TouchableOpacity>
</View>
);
};
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={Home} options={headerOptions} />
<Stack.Screen
name="IconScreen"
component={IconScreen}
options={headerOptions}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
const styles = StyleSheet.create({
actionPrimary: {
width: 56,
height: 56,
marginRight: 6,
},
});
export default App;
以上设定完後,简单的切页、换页传递资料及回上一页就完成了!!
页面传递资料可以使用 navigation.navigate(A ,{ becca: "卡卡" }), 而到 A 页面要读取的话使用 route.params.becca="卡卡"
其实 options 还有包含像是 native 的换页动画及手势~ 大家可以再参考官方 doc 研究一下!
Day 15 done! 请多多指教~
>>: javascript函式与动态变更网页内容(DAY17)
今天来学学Vue里面的判别式v-if 跟v-show 1.v - if 在这里我们将条件设定为Sho...
昨天有说到div因为block这个元素而占了整行无法并排,今天就来说一下block是甚麽样的元素 d...
为什麽要选这个主题??? 原因就是我在某某补习班的主修就是这个~ 付出学费当然就是要验收阿~ Goo...
如果有错误,欢迎留言指教~ Q_Q State 的更新可能是非同步的!? 如果想要连续执行更新 s...
「每一个范式都将某些东西带离开我们。goto语句、函式指标、赋值,还有什麽东西可以带走的吗?」 「...