如何将数据从一个屏幕传递到React Native中的其他屏幕

时间:2016-09-27 09:45:14

标签: react-native react-native-android

1。 index.android.js

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

import Button from 'react-native-button';

class AwesomeProject extends Component {

constructor(props){
super(props)

this.state = {
  username: '',
  email: '',
  address: '',
  mobileNumber: ''
 }


  render() {
    return (
      <View style={styles.container}>

      <TextInput
      ref={component => this.txt_input_name = component}
      style={styles.textInputStyle}
      placeholder="Enter Name"
      returnKeyLabel = {"next"}
      onChangeText={(text) => this.setState({username:text})}
      />

      <Button style={styles.buttonStyle}>
      Submit
      </Button> 
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
 buttonStyle: {
   alignSelf: 'center',
   textAlign: 'center',
   color: '#FFFFFF', 
   fontSize:18,
   marginTop:20,
   marginBottom:20,
   padding:5,
   borderRadius:5,
   borderWidth:2,
   width:100,
   alignItems: 'center',
   backgroundColor:'#4285F4',
   borderColor:'#000000'
}
});

AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);

2 个答案:

答案 0 :(得分:2)

您需要使用导航器

将道具传递到另一个页面
 this.props.navigator.push({
    name: 'Home',
    passProps: {
      name: property
    }
  })

我从此链接https://medium.com/@dabit3/react-native-navigator-navigating-like-a-pro-in-react-native-3cb1b6dc1e30#.6yrqn523n

获取此信息

答案 1 :(得分:0)

首先

import {Navigator}

中定义导航器
  

呈现

的功能

  

index.android.js

像这样

render() {
    return (
      <Navigator
          initialRoute={{id: 'HomePage', name: 'Index'}}
          renderScene={this.renderScene.bind(this)}
          configureScene={(route) => {
            if (route.sceneConfig) {
              return route.sceneConfig;
            }
            return Navigator.SceneConfigs.FloatFromRight;
          }} />
    );
}

然后定义

  

渲染功能

像这样

renderScene(route, navigator) {
    var routeId = route.id;
    if (routeId === 'HomePage') {
      return (
        <HomePage
          navigator={navigator} />
      );
    }
    if (routeId === 'DetailPage') {
      return (
        <DetailPage
          navigator={navigator}

          {...route.passProps} 
          />
      );
    }

}

指定属性

  

{...} route.passProps

用于将值传递到屏幕。我把它放在DetailPage

里面

然后你可以使用

  

passProps

用于调用下一页。在我的情况下

  

DetailPage

this.props.navigator.push({
      id: 'DetailPage',
      name: 'DetailPage',
      passProps: {
      name:value

    }
});