在react native中将值从一个js文件传递到另一个js文件:react native

hrirmatl  于 2022-11-30  发布在  React
关注(0)|答案(2)|浏览(149)

我有这个parrentbaby_login文件,它有一个google注册按钮,这是一个文件名为Sign的组件,当我单击按钮时,我希望userInfosign.js传递到Signupfor.js我如何实现这一点?

export default function Baby_login() {
  return (
    <View style={styles.prheight}>
      <Image
        source={require('../assets/images/mother.png')}
        style={{
          width: 300,
          marginLeft: 20,
          marginTop: 0,
          justifyContent: 'center',

          height: 300,
          textAlign: 'center',
        }}
      />
      <View style={styles.buttonw}>
        <Sign />
      </View>
    </View>
  );
}

签名.js

export default function Sign(navigation) {
  
  async function onGoogleButtonPress() {
    await GoogleSignin.hasPlayServices();
    const userInfo = await GoogleSignin.signIn();
    console.log(userInfo);
    setuserInfo(userInfo);
   
  }
  return (
    <View style={styles.prheight}>
      <View style={styles.buttonw}>
        <GoogleSigninButton
          style={{width: 192, height: 48}}
          size={GoogleSigninButton.Size.Wide}
          color={GoogleSigninButton.Color.Light}
          onPress={onGoogleButtonPress}
          // disabled={this.state.isSigninInProgress}
        />
      </View>
    </View>
  );
}

注册.js

import React from 'react';

export default function Signupfor(userInfo) {
  return <View style={styles.prheight}></View>;
}

应用程序.js

在这里我已经更新了app.js文件,这样你就可以看到导航细节

import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {Provider as PaperProvider} from 'react-native-paper';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {StyleSheet, Text, View} from 'react-native';
import Baby from './components/baby_login';
import {Avatar, Card, Title, Paragraph} from 'react-native-paper';
import {Image} from 'react-native';
import Signupfor from './components/signupfor';
import Pregnant from './components/pregent_login';

import {Button} from 'react-native';

function Dashbord({navigation}) {
  const LeftContent = props => <Avatar.Icon {...props} icon="folder" />;
  return (
    <View style={styles.main}>
      <Title
        style={{
          textAlign: 'center',
          marginBottom: 30,
          fontSize: 28,
          fontFamily: 'Poppins-ExtraBold',
        }}>
        Tell us about you
      </Title>
      <Card
        style={styles.main2}
        onPress={() => navigation.navigate('Pregnant')}>
        <Image
          source={require('./assets/images/pregnant.png')}
          style={{
            width: 80,
            marginLeft: 90,
            marginTop: 0,
            justifyContent: 'center',

            height: 80,
            textAlign: 'center',
          }}
        />
        {/* <Image source={require('./assets/images/pregnant.png')} /> */}
        <Text
          style={{
            textAlign: 'center',
            fontSize: 20,
            fontFamily: 'Poppins-ExtraBold',
          }}>
          I am pregnant
        </Text>
      </Card>
      <Card style={styles.main2} onPress={() => navigation.navigate('Baby')}>
        <Image
          source={require('./assets/images/mother.png')}
          style={{
            width: 80,
            marginLeft: 90,
            marginTop: 0,
            justifyContent: 'center',

            height: 80,
            textAlign: 'center',
          }}
        />
        <Text
          style={{
            textAlign: 'center',
            fontSize: 20,
            fontFamily: 'Poppins-ExtraBold',
          }}>
          i am a mother
        </Text>
      </Card>
    </View>
  );
}

function HomeScreen({navigation}) {
  return (
    <View style={styles.prheight}>
      <View>
        <Text style={styles.r}>home</Text>
      </View>
      <View style={styles.buttonw}>
        <Button
          color="#7743DB"
          title="Lets Go"
          onPress={() => navigation.navigate('Dashbord')}
        />
      </View>
    </View>
  );
}

const Stack = createNativeStackNavigator();

function App() {
  return (
    <PaperProvider>
      <NavigationContainer>
        <Stack.Navigator>
          <Stack.Screen
            options={{headerShown: false}}
            name="Home"
            component={HomeScreen}
          />
          <Stack.Screen name="Dashbord" component={Dashbord} />
          <Stack.Screen name="Baby" component={Baby} />
          <Stack.Screen name="Pregnant" component={Pregnant} />
          <Stack.Screen name="Signupfor" component={Signupfor} />
        </Stack.Navigator>
      </NavigationContainer>
    </PaperProvider>
  );
}
tkqqtvp1

tkqqtvp11#

你是如何尝试导航的?我想在onGoogleButtonPress响应时,你想导航到下一个包含userInfo数据的屏幕。

您需要为此使用React导航库。

export default function Sign({navigation}) {
  
  async function onGoogleButtonPress() {
    await GoogleSignin.hasPlayServices();
    const userInfo = await GoogleSignin.signIn();
    console.log(userInfo);
    setuserInfo(userInfo);
    navigation.navigate("Signupfor", { userInfo })

  }
  return (
    <View style={styles.prheight}>
      <View style={styles.buttonw}>
        <GoogleSigninButton
          style={{width: 192, height: 48}}
          size={GoogleSigninButton.Size.Wide}
          color={GoogleSigninButton.Color.Light}
          onPress={onGoogleButtonPress}
          // disabled={this.state.isSigninInProgress}
        />
      </View>
    </View>
  );
}

注册.js

import React from 'react';

export default function Signupfor(props) {

  const {userInfo} = props?.route?.params

  return <View style={styles.prheight}></View>;
}

我希望这对你有帮助,谢谢!

bxjv4tth

bxjv4tth2#

这里Sign是组件而不是堆栈屏幕,所以我们必须从Baby_login传递导航

export default function Baby_login(props) {
  return (
    <View style={styles.prheight}>
      <Image
        source={require('../assets/images/mother.png')}
        style={{
          width: 300,
          marginLeft: 20,
          marginTop: 0,
          justifyContent: 'center',

          height: 300,
          textAlign: 'center',
        }}
      />
      <View style={styles.buttonw}>
        <Sign navigation={props.navigation} />
      </View>
    </View>
  );
}

单位:Sign

export default function Sign(navigation) {
  
  async function onGoogleButtonPress() {
    await GoogleSignin.hasPlayServices();
    const userInfo = await GoogleSignin.signIn();
    console.log(userInfo);
    setuserInfo(userInfo);
    navigation.navigate("Signupfor", { userInfo })
   
  }
  return (
    <View style={styles.prheight}>
      <View style={styles.buttonw}>
        <GoogleSigninButton
          style={{width: 192, height: 48}}
          size={GoogleSigninButton.Size.Wide}
          color={GoogleSigninButton.Color.Light}
          onPress={onGoogleButtonPress}
          // disabled={this.state.isSigninInProgress}
        />
      </View>
    </View>
  );
}

Signupfor为单位

import React from 'react';

export default function Signupfor(props) {
  const {userInfo} = props?.route?.params
  return <View style={styles.prheight}></View>;
}

相关问题