React Native 如何通过按钮onPress实现页面跳转 跳转报错
代码核心问题
目前的代码完全跑不通,所有会直接触发报错的点列在这里:
- 没有集成页面导航系统:React Native本身没有内置的页面跳转能力,必须通过导航库定义好所有页面的路由映射,哪怕import了Event页面的组件,没有路由注册也根本跳不过去
- 函数调用错误:App.js里给按钮绑的onPress触发
this.onPress,但整个App类从来没写过onPress方法,点按钮直接报“undefined is not a function” - 逻辑位置错放:把跳转用的
navigateToevent方法写在了Event目标页的组件里,首页根本访问不到这个方法,写了等于白写 - 组件命名不规范:自定义组件用了小写开头的
event当类名,JSX解析时会把它当成浏览器内置的原生标签处理,直接触发组件识别错误 - 依赖未导入:Event页用了
DropDownPicker组件,既没安装对应的第三方库也没导入,渲染到这一步直接报组件未定义 - 布局组件误用:Event页渲染层直接用Fragment包两个组件,RN环境下Fragment没有布局属性,会导致下拉选择器被上层元素遮挡的异常
可直接运行的修正步骤
- 先安装必备依赖(目前RN社区通用的跳转方案基于React Navigation)
# 安装导航核心模块 npm install @react-navigation/native @react-navigation/native-stack # 安装导航依赖的原生基础模块 npm install react-native-screens react-native-safe-area-context # 如果需要下拉选择器,单独安装对应组件库 npm install react-native-dropdown-picker
- 调整App.js的代码,把导航容器、路由配置、首页逻辑整合,修正按钮跳转逻辑:
import React from "react"; import { StyleSheet, Text, View, Button, Image } from "react-native"; import { NavigationContainer } from "@react-navigation/native"; import { createNativeStackNavigator } from "@react-navigation/native-stack"; // 导入Event页面,注意导入后要把组件名改成首字母大写 import Event from './event.js'; // 单独抽离首页组件 function HomeScreen({ navigation }) { return ( <View> <Text style={styles.h1}>WELCOME TO </Text> <Image style={styles.logo} source={require('./logo_tiny.jpg')} /> <View style={styles.container}> <View style={styles.b1}> <Button title="Events" // 直接用导航对象提供的方法跳转已注册的Event路由 onPress={() => navigation.navigate('Event')} color="#fff" /> </View> <View style={styles.b1}> <Button title="Package" onPress={() => {}} color="#fff" /> </View> </View> <View style={styles.container}> <View style={styles.b1}> <Button title="Promotion" onPress={() => {}} color="#fff" /> </View> <View style={styles.b1}> <Button title="Support" onPress={() => {}} color="#fff" /> </View> </View> </View> ) } // 创建导航栈实例 const Stack = createNativeStackNavigator(); // 项目入口组件,套上导航容器,注册所有路由 export default function App() { return ( <NavigationContainer> <Stack.Navigator initialRouteName="Home"> {/* 注册首页路由 */} <Stack.Screen name="Home" component={HomeScreen} /> {/* 注册Event页面路由 */} <Stack.Screen name="Event" component={Event} /> </Stack.Navigator> </NavigationContainer> ) } // 样式部分和原有逻辑完全保留,不用改动 const styles = StyleSheet.create({ h1: { textAlign: 'center', color: '#1E7778', fontSize: 32, marginTop: 18, width: 400, fontWeight: 'bold', }, container: { flexDirection: 'row', }, b1: { width: '40%', margin: 20, padding: 10, backgroundColor: '#D16B11', borderRadius: 20, }, logo: { alignItems: 'center', justifyContent:'center', left: '20%', }, });
- 调整event.js的代码,修正命名、删掉冗余逻辑、补全组件导入和布局结构:
import React from 'react'; // 补全DropDownPicker的导入(对应上面安装的第三方下拉组件) import DropDownPicker from 'react-native-dropdown-picker'; // 类名改成首字母大写,符合JSX组件识别规范 export default class Event extends React.Component { // 删掉原来写在这里的navigateToevent方法,跳转逻辑不需要写在目标页内 constructor(props) { super(props); this.state = { country: null, city: null, cities: [] }; } changeCountry(item) { let city = null; let cities = []; switch (item.value) { case 'fr': cities = [ {label: 'Paris', value: 'paris'} ]; break; case 'es': cities = [ {label: 'Madrid', value: 'madrid'} ]; break; } this.setState({ city, cities }); } changeCity(item) { this.setState({ city: item.value }); } render() { return ( <View> <DropDownPicker items={[ {label: 'France', value: 'fr'}, {label: 'Spain', value: 'es'}, ]} defaultNull placeholder="Select your country" containerStyle={{height: 40}} onChangeItem={item => this.changeCountry(item)} /> <DropDownPicker items={this.state.cities} defaultNull={this.state.city === null} placeholder="Select your city" containerStyle={{height: 40}} onChangeItem={item => this.changeCity(item)} /> </View> ); } }
小提醒:其他几个暂时没做逻辑的按钮,现在绑的onPress是空函数,等后续做其他页面的时候,按照同样的方式在导航栈注册对应路由,再用
navigation.navigate('路由名')就能跳转。
内容的提问来源于stack exchange,提问作者Anu
相关产品推荐
相关产品推荐

