简体   繁体   English

如何在反应导航中导航和传递同一组件的道具?

[英]How to navigate and passing props at the same component in react navigation?

I tried to navigate my App component to GamePlay component, here's my App.js looks like.我试图将我的App组件导航到GamePlay组件,这是我的App.js的样子。

import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';


// Import my own components
import GamePlay from './components/GamePlay';
import {data} from './components/Stage/Stage';

const Stack = createStackNavigator();


export default function App() {
  return (
      <NavigationContainer>
        <Stack.Navigator
          screenOptions={{
            headerShown: false
          }}
        > 

            <Stack.Screen name="Game Play">
              {() => <GamePlay data={data}/>}
            </Stack.Screen>
        
        </Stack.Navigator>
      </NavigationContainer>
  );
}

and this is my GamePlay component looks like.这是我的GamePlay组件的样子。

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

export default function GamePlay({navigation}, props) {

    const ToHome = () => {
        navigation.navigate("Home Screen")
    }

    return (
        <View>
            <TouchableOpacity onPress={ToHome}>
                <Text>Go Home</Text>
            </TouchableOpacity>
            <View>
                <Text>this is gameplay</Text>
            </View>
        </View>
    )
}

but why when I do console.log(props.data) in the GamePlay component It says undefined ?但是为什么当我在 GamePlay 组件中执行console.log(props.data)时它说undefined I'm confuse how to pass navigation with props in a component.我很困惑如何通过组件中的道具传递导航。

Small working example: Expo snack小工作示例: Expo 小吃

try this:尝试这个:

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

export default 
function GamePlay({navigation, data}) {
    console.log(data)
    const ToHome = () => {
        navigation.navigate("Home Screen")
    }

    return (
        <View>
            <TouchableOpacity onPress={ToHome}>
                <Text>Go Home</Text>
            </TouchableOpacity>
            <View>
                <Text>this is gameplay</Text>
            </View>
        </View>
    )
}

also avoid keeping space while naming screens.还避免在命名屏幕时保留空间。

I'm guessing you're trying to pass props from when you declare the route in the stack.我猜你是在尝试在堆栈中声明路由时传递道具。

You can use initialParams for that like so:您可以像这样使用 initialParams :

<Stack.Screen component={GamePlay} name="GamePlay" initialParams={{data:data}} />

then write your GamePlay component as such:然后像这样编写您的 GamePlay 组件:

 import React from 'react'; import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; export default function GamePlay({navigation, route}, props) { const ToHome = () => { navigation.navigate("Home Screen") } return ( <View> <TouchableOpacity onPress={ToHome}> <Text>Go Home</Text> </TouchableOpacity> <View> <Text>this is gameplay</Text> </View> </View> ) }

and console.log(route.params).和 console.log(route.params)。 Should work just fine.应该工作得很好。

navigation.navigate("Home Screen",{param1:'123456'}) navigation.navigate("主屏幕",{param1:'123456'})

try to put {navigation,data} insted of {navigation}, props)尝试把 {navigation,data} insted of {navigation}, props)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM