开发者问题收集

React Navigation 5 route.params 未定义,即使我提供了一个 prop

2020-03-14
9220

我目前陷入了 React 导航问题,我也查阅了文档,似乎我设置正确,但我可能遗漏了一些东西。我有一个按钮正在路由到主屏幕并传递一个 prop

import React, { useState } from 'react';
import { View, Text, TextInput, Button, StyleSheet, Picker } from 'react-native';
// not sure if i need to import navigation? It seems to work since it's a screen

const LevelScreen = ({ navigation }) => {
    const [ gridSize, setGridAmount ] = useState(3);

    return (
             // OTHER CODE THAT SHOULDN'T REALLY HAVE AN EFFECT ON THIS

             <Button
                title='Continue'
                // this should navigate to the mainScreen
                // needs to pass in row and column 
                onPress={() => navigation.navigate(
                  'Game', { gridSizeVal: 3 }
                )}
            />

这是我要导航到的屏幕:

import React, { useState, useEffect } from 'react';
import { View, StyleSheet, Text, Button, Alert } from 'react-native';
import Grid from '../components/Grid';
import ConsoleBox from '../components/ConsoleBox';
import COLORS from '../core/commonText';
// Not sure if i need to import navigation from react/react-native? 

const GameScreen = ({ route, navigation }) => {
    const { gridSizeValue } = route.params;
    // The rest of my code 

但它一直响应 TypeError:未定义不是对象(评估“route.params”) 我尝试同时使用 route.params.gridSizeValueroute.params.gridSizeValue.name ,但它们都出现相同的错误。我目前使用的是 react-navigation/native 的 版本 5.0.9 。我咨询过 https://reactnavigation.org/docs/params ,似乎我除了使用箭头函数代替显式函数外,其他都做了,而且我还在学习 react-native,所以可能没有任何区别?如果这是一个重复的问题,我很乐意回答这个问题,但我似乎找不到任何东西。谢谢!

编辑:我运行了 console.log(route) ,它返回了 undefined 。我也尝试使用 onPress={() => this.props.navigation.navigate('Game', { gridSizeVal: 3}) ,但出现了 typeError: Undefined is not an object (evalutating '_this.props.navigation')

我还发现我的 App.jsindex.js 可能与此有关,因此这是 App.js

import 'react-native-gesture-handler';
import { createAppContainer } from 'react-navigation';
import GameScreen from './src/screens/GameScreen';
import { createStackNavigator } from 'react-navigation-stack';
import LevelScreen from './src/screens/LevelScreen';

const navigator = createStackNavigator(
  {
    Level: LevelScreen,
    Game: GameScreen
  },
  {
    initialRouteName: 'Level',
    defaultNavigationOptions: {
      title: 'Game Of Seasons'
    }
  }
);

export default createAppContainer(navigator);

而我的 index.js 是:

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);
3个回答

您正在导入 react-navigation 4.x 版本,但您正在遵循 5.x 的文档。解构的 {route 在 4.x 中不可用(而是传入的参数来自 navigation.state.params )。

有关更多详细信息,请参阅官方文档: https://reactnavigation.org/docs/upgrading-from-4.x

cchapin
2021-03-15

如果您的堆栈导航器是根导航器,那么您可以轻松地将参数传递给其他屏幕(与当前屏幕同一级别),如下所示。

<Button onPress={() => navigation.navigate('nameComponentInStack', {screen: 'ScreenName', params: {paramName: paramValue}})}
za_ali33
2021-03-12

因此答案最终是 navigation.state.params.gridSizeVal ,其代码块是:

const GameScreen = ({ navigation }) => {
    const [ gridSize, setGridSize ] = useState(navigation.state.params.gridSizeVal);

我遇到的一个问题是,在此示例中,我无法将 prop 分配给一个值:

let { gridSizeValue } = navigation.state.params.gridSizeVal;

控制台记录上述内容返回 undefined ,但这解决了我通过导航堆栈获取参数的问题。

Spikes
2020-03-20