0

我想将值“Hello”从 App.js 传递给 screen1.js。小吃:https ://snack.expo.io/@mobshed/passing-data-between-functions

谢谢您的帮助!

应用程序.js

import React, { useState, useEffect, Component } from "react";
import MyScreen from './screen1'

export default function Root() {
    return <MyScreen show="Hello" />;
}

screen1.js

import { NavigationContainer } from "@react-navigation/native";
import {
  createStackNavigator
} from "@react-navigation/stack";
const Stack = createStackNavigator();

function screenPage ({show}) {
  alert(show)
  return (<View style={{ justifyContent: "center", flex: 1}}><Text>Hello world</Text></View>)
}

export default function Root({ navigation }) {
  return (
    <NavigationContainer>
    <Stack.Navigator
      screenOptions={{
        headerTitleAlign: "center",
        gestureEnabled: false,
        gestureDirection: "horizontal"
      }}
    >
      <Stack.Screen
        options={{
          headerShown: false
        }}
        name="screenPage"
        component={screenPage}
      />   
    </Stack.Navigator>
     </NavigationContainer>
  );
}
4

2 回答 2

2

在 screen1.js 中:

import { NavigationContainer } from "@react-navigation/native";
import {
  createStackNavigator
} from "@react-navigation/stack";
const Stack = createStackNavigator();

function screenPage ({route}) {
  const {show} = route.params;
  alert(show);
  return (<View style={{ justifyContent: "center", flex: 1}}><Text>Hello world</Text></View>)
}

export default function Root({ navigation, show }) {
  return (
    <NavigationContainer>
    <Stack.Navigator
      screenOptions={{
        headerTitleAlign: "center",
        gestureEnabled: false,
        gestureDirection: "horizontal"
      }}
    >
      <Stack.Screen
        options={{
          headerShown: false
        }}
        name="screenPage"
        component={screenPage}
        initialParams={{show: show}}
      />   
    </Stack.Navigator>
     </NavigationContainer>
  );
}

另一种方式,你可以传递这样的参数:

<Button
        title="Done"
        onPress={() => {
          // Pass params back to screenPage
          navigation.navigate('screenPage', { show: "hello" });
        }}
      />
于 2020-03-05T04:33:32.253 回答
1

好的,找到了!感谢 Khanh 在您删除答案之前让我走上了正确的道路

import { NavigationContainer } from "@react-navigation/native";
import {
  createStackNavigator
} from "@react-navigation/stack";
const Stack = createStackNavigator();

function screenPage ({route}) {
  alert(route.params.user)
  return (<View style={{ justifyContent: "center", flex: 1}}><Text>Hello world</Text></View>)
}

export default function Root({ navigation, show}) {
  return (
    <NavigationContainer>
    <Stack.Navigator
      screenOptions={{
        headerTitleAlign: "center",
        gestureEnabled: false,
        gestureDirection: "horizontal"
      }}
    >
      <Stack.Screen
        options={{
          headerShown: false
        }}
        name="screenPage"
        component={screenPage}
        initialParams={{ user: show }}
      />   
    </Stack.Navigator>
     </NavigationContainer>
  );
}
于 2020-03-05T04:59:21.953 回答