使用React-Native Navigation传递数据 [英] Passing Data Using React-Native Navigation

查看:338
本文介绍了使用React-Native Navigation传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to pass data between screens in my app. Currently I am using

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

我有我的index.ios

I have my index.ios

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);

app as

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

然后秒屏为

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

import { StackNavigator } from 'react-navigation';


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


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

    </View>
  );
}

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

export default SecondScreen

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

Whenever I console.log I get undefined.
https://reactnavigation.org/docs/navigators/navigation-prop The docs say every screen should have these values what am I doing wrong?

推荐答案

在你的代码中, props.navigation和this.props.navigation.state是两回事。你应该在第二个屏幕上试试这个:

In your code, props.navigation and this.props.navigation.state are two different things. You should try this in your second screen:

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

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

the const {state} line is only here to get an easy to read code.

这篇关于使用React-Native Navigation传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆