简体   繁体   English

我如何在 React Native 的屏幕之间传递数据

[英]How I can pass data between screens in React Native

I'm trying to passing data between screen and failed with我试图在屏幕之间传递数据但失败了

undefined is not an object (evaluating 'props.navigation.getParams') 

My code:我的代码:

First screen:第一个画面:

const ForgotPasswordScreen = ({ navigation, }) => {
  const [text, setText] = useState('');
  return (
    <View>
      <TextInput onChangeText={(text) => { setText(text) }}></TextInput>
      <TouchableOpacity onPress={() => { navigation.navigate('VerificationScreen', { phoneNumber: text }) }}>
         <Text style={{ color: COLORS.WHITE, fontSize: 16, fontWeight: 'bold' }}>Continue</Text>
      </TouchableOpacity>
    </View>
  )

And i'm try to receive data at second screen:我尝试在第二个屏幕接收数据:

const VerificationScreen = ({ navigation, }, props) => {
  const phoneNumber = props.navigation.getParams('phoneNumber');
  return (
    <View>
       <Text>{phoneNumber}</Text>
    </View>
  )

Thanks so much !!非常感谢 !!

In react, props is the first argument of a function component.在 react 中, props是函数组件的第一个参数。

In your example above, you attempt to access props in the second argument of the VerificationScreen function component (which would be undefined ).在上面的示例中,您尝试访问VerificationScreen函数组件的第二个参数中的props (将是undefined )。

Furthermore, you attempt to access props.navigation which would give you an error:此外,您尝试访问props.navigation这会给您一个错误:

Uncaught TypeError: Cannot read property 'navigation' of undefined未捕获的类型错误:无法读取未定义的属性“导航”

Instead, because you are already de-structuring navigation from props in the first argument of the VerificationScreen function component, you should use it to access the navigate method.相反,因为您已经在VerificationScreen函数组件的第一个参数中从props navigation ,所以您应该使用它来访问navigate方法。

const ForgotPasswordScreen = ({ navigation }) => {
  const [text, setText] = useState('');
  return (
    <View>
      <TextInput onChangeText={(text) => { setText(text) }}></TextInput>
      <TouchableOpacity onPress={() => { navigation.navigate('VerificationScreen', { phoneNumber: text }) }}>
         <Text>Continue</Text>
      </TouchableOpacity>
    </View>
  )



const VerificationScreen = ({ navigation }) => {
  const phoneNumber = navigation.getParams('phoneNumber');
  return (
    <View>
       <Text>{phoneNumber}</Text>
    </View>
   )
}

I highly recommend that you take some time to read the react docs .我强烈建议您花一些时间阅读react 文档

Output:输出:

在此处输入图片说明

This full example should work:这个完整的例子应该有效:


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

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

const Stack = createStackNavigator();

const App = ({ navigation }) => {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="ForgotPassword" component={ForgotPasswordScreen} />
        <Stack.Screen name="Verification" component={VerificationScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

const VerificationScreen = ({ navigation, route }) => {
  const { phoneNumber } = route.params;
  return (
    <View>
      <Text>{phoneNumber}</Text>
    </View>
  );
};

const ForgotPasswordScreen = ({ navigation }) => {
  const [text, setText] = useState('');
  const handleInput = (event) => {
    let temp = event.nativeEvent.text;
    setText(temp);
    console.log(temp)
  };
  return (
    <View style={styles.container}>
      <TextInput
        placeholder={'Input Text'}
        value={text}
        onChange={handleInput}
        style={styles.input}
      />
      <TouchableOpacity
        onPress={() =>
          text
            ? navigation.navigate('Verification', { phoneNumber: text })
            : alert('Please Input the text')
        }>
        <View style={styles.btn}>
          <Text style={styles.text}>NEXT</Text>
        </View>
        <Text>{text}</Text>
      </TouchableOpacity>
    </View>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    marginTop: 100,
  },
  btn: {
    width: 100,
    height: 50,
    backgroundColor: 'white',
    borderRadius: 10,
    border: '2px solid black',
    justifyContent: 'center',
    alignItems: 'center',
  },
  input: {
    borderRadius: 10,
    color: 'black',
    width: 300,
    padding: 10,
    border: '2px solid green',
    marginVertical: 5,
  },
  text: {
    fontWeight: 'bold',
  },
});

You can find a full live example here: Live Demo您可以在此处找到完整的现场示例:现场演示

in second screen you can change在第二个屏幕中,您可以更改

const VerificationScreen = (route, navigation) => {
  const { phoneNumber } = route.params;;
  return (
    <View>
       <Text>{phoneNumber}</Text>
    </View>
  )

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

相关问题 如何在 React Native Expo 项目中的屏幕之间导航? - How can I navigate between screens in a React Native Expo project? React Native 在两个屏幕中的两个 class 组件之间传递数据 - React Native pass data between two class component in two screens 如何在反应原生的屏幕之间传递变量(见附件) - How to pass variable between screens in react native (see attached) react-native 如何在两个屏幕之间传递参数? - How to pass parameters between two screens in react native? 如何在 React Native 中跨屏幕传递 API (Axios) 响应数据? - How to pass API (Axios) Response data across screens in React Native? 如何在不使用 navigation.navigate() 的情况下在 React Native 中的屏幕之间导航? - How can I navigate between screens in React Native without using navigation.navigate()? 如何在 React Native 屏幕之间共享状态? - How do I share state between react native screens? 如何使用 React Native 创建动态路由/屏幕? - How can I create dynamic routes/ screens with React Native? 在 React Native 中的两个不同屏幕之间传递数据的正确方法是什么? - What is the correct way to pass data between two different screens in React Native? 如何在反应本机导航中传递数据? - How can I pass data in react native navigation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM