You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native页面间传值报错,点击搜索按钮触发,求解决方案

问题解决:React Native 页面传值报错

错误原因分析

你的代码存在两个核心问题导致传值失败:

  1. 组件参数接收方式错误:SubScreen1 直接将 route 作为函数参数,但 React Native 函数组件的入参是包含 route 的 props 对象,需要通过解构获取 route 属性。
  2. 参数解构逻辑错误:你尝试从 route.params.paramKey 中解构 paramKey,但实际传参格式为 {paramKey: value},paramKey 直接挂载在 route.params 对象下,无需嵌套解构。

修正后的 SubScreen1 代码

import { StyleSheet, Text, View } from 'react-native'
import React from 'react'

// 解构获取 props 中的 route
const SubScreen1 = ({ route }) => {
  // 直接从 route.params 中解构目标参数
  const { paramKey } = route.params
  console.log(paramKey)
  return (
    <View>
      <Text>{paramKey}</Text>
    </View>
  )
}

export default SubScreen1

const styles = StyleSheet.create({})

额外优化建议

  • 避免空值传递:你的 value 初始值为 null,若用户未选择下拉选项就点击搜索,会传递空值到目标页面,可在跳转前增加校验:
<TouchableOpacity
    onPress={() => {
      value ? navigation.navigate("SubScreen1", {paramKey: value}) : alert('请先选择菜系')
    }}
    style = {styles.button}
>
  <Text style = {styles.buttonText}>Search</Text>
</TouchableOpacity>
  • 清理冲突依赖:你的依赖中同时存在新版 @react-navigation/native-stack(v6)和旧版 react-navigation(v4),建议移除旧版依赖避免版本冲突:
"dependencies": {
    "@react-native-community/masked-view": "^0.1.11",
    "@react-navigation/native": "^6.0.12",
    "@react-navigation/native-stack": "^6.8.0",
    "expo": "~46.0.9",
    "expo-status-bar": "~1.4.0",
    "firebase": "^9.9.4",
    "react": "18.0.0",
    "react-native": "0.69.6",
    "react-native-dropdown-picker": "^5.4.2",
    "react-native-gesture-handler": "~2.5.0",
    "react-native-reanimated": "~2.9.1",
    "react-native-safe-area-context": "4.3.1",
    "react-native-screens": "~3.15.0"
  },

内容的提问来源于stack exchange,提问作者L.Calvin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 03:11:52