使用 React-Native 导航传递数据

IT技术 javascript android ios reactjs react-native
2021-04-09 03:43:49

我试图在我的应用程序的屏幕之间传递数据。目前我正在使用


"react-native": "0.46.0",
"react-navigation": "^1.0.0-beta.11"

我有我的 index.js


 import React, { Component } from 'react';
    import {
      AppRegistry,
    } from 'react-native';
    import App from './src/App'
    import { StackNavigator } from 'react-navigation';
    import SecondScreen from './src/SecondScreen'    

    class med extends Component {
      static navigationOptions = {
        title: 'Home Screen',
      };

      render(){
        const { navigation } = this.props;

        return (
          <App navigation={ navigation }/>
        );
      }
    }

    const SimpleApp = StackNavigator({
      Home: { screen: med },
      SecondScreen: { screen: SecondScreen, title: 'ss' },    
    });

    AppRegistry.registerComponent('med', () => SimpleApp);

应用程序作为

    import React, { Component } from 'react';
    import {
      StyleSheet,
      Text,
      Button,
      View
    } from 'react-native';
    import { StackNavigator } from 'react-navigation';

    const App = (props)  => {
      const { navigate } = props.navigation;

      return (
        <View>
          <Text>
            Welcome to React Native Navigation Sample!
          </Text>
          <Button
              onPress={() => navigate('SecondScreen', { user: 'Lucy' })}
              title="Go to Second Screen"
            />
        </View>
      );
    }

    export default App

然后在 secondscreen.js 中我们将获取从前一个屏幕传递的数据作为


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

    import { StackNavigator } from 'react-navigation';


    const SecondScreen = (props)  => {
      const { state} = props.navigation;
      console.log("PROPS" + state.params);


      return (
        <View>
          <Text>
            HI
          </Text>

        </View>
      );
    }

    SecondScreen.navigationOptions = {
      title: 'Second Screen Title',
    };

    export default SecondScreen

每当我 console.log 我得到未定义。
https://reactnavigation.org/docs/navigators/navigation-prop 文档说每个屏幕都应该有这些值我做错了什么?

6个回答

在你的代码中,props.navigationthis.props.navigation.state是两个不同的东西。您应该在第二个屏幕中尝试此操作:

const {state} = props.navigation;
console.log("PROPS " + state.params.user);

const {state}行只是为了获得易于阅读的代码。

你如何动态地做到这一点?所以我想将一组对象从一个屏幕传递到另一个屏幕,这是我们可以做的吗?或将所选对象的数据传递到下一个屏幕?非常感谢!!!
2021-06-12 03:43:49

所有其他答案现在似乎已经过时。在当前的 React 导航版本 ( "@react-navigation/native": "^5.0.8",) 中,您首先在一个屏幕之间传递值,如下所示:

       function HomeScreen({ navigation }) {
      return (
        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
          <Text>Home Screen</Text>
          <Button
            title="Go to Details"
            onPress={() => {
              /* 1. Navigate to the Details route with params, passing the params as an object in the method navigate */
              navigation.navigate('Details', {
                itemId: 86,
                otherParam: 'anything you want here',
              });
            }}
          />
        </View>
      );
    }

然后在您正在重定向的组件中,您会获得像这样传递的数据:

function DetailsScreen({ route, navigation }) {
  /* 2. Get the param */
  const { itemId } = route.params;
  const { otherParam } = route.params;
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Details Screen</Text>
      <Text>itemId: {JSON.stringify(itemId)}</Text>
      <Text>otherParam: {JSON.stringify(otherParam)}</Text>
    </View>
  );
}

所以,基本上,现在的数据在里面this.props.route.params在上面的那些例子中,我展示了如何从功能组件中获取它们,但在类组件中是类似的,我做了这样的事情:

首先我从这个 ProfileButton 传递数据,在它的handleNavigate函数中,像这样:


// these ProfileButton and ProfileButtonText, are a Button and a Text, respectively,
// they were just styled with styled-components 
<ProfileButton
 onPress={() => this.handleNavigate(item) 
  <ProfileButtonText>
      check profile
  </ProfileButtonText>
</ProfileButton>

其中handleNavigate是这样的:

   handleNavigate = user => {
        // the same way that the data is passed in props.route,
        // the navigation and it's method to navigate is passed in the props.
        const {navigation} = this.props;
        navigation.navigate('User', {user});
    };

然后,函数 HandleNavigate 重定向到用户页面,这是一个类组件,我得到这样的数据:

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

export default class User extends Component {
    state = {
        title: this.props.route.params.user.name,
    };


    render() {
        const {title} = this.state;
        return (
            <View>
                <Text>{title}</Text>
            </View>
        );
    }
}

在类组件中,我发现的方法是使这条线很长, title: this.props.route.params.user.name,但它有效。如果有人知道如何在当前版本的 react-native 导航中缩短它,请赐教。我希望这能解决你的问题。

头等舱

<Button onPress = {
  () => navigate("ScreenName", {name:'Jane'})
} />

二等舱

const {params} = this.props.navigation.state

react导航 3.*

家长班

this.props.navigation.navigate('Child', {
    something: 'Some Value',
});

儿童班

this.props.navigation.state.params.something // outputs "Some Value"

您可以访问您的PARAM是user,与props.navigation.state.params.user相关组件(SecondScreen)。

这也有效,谢谢我只是缺少参数。
2021-05-30 03:43:49