如何使用Navigator英寸的React Native将数据从sceneA传递到sceneB?

我用这个go sceneB:

this.props.navigator.push({
    id: "MainPage",
    name: 'mainPage'
});

推荐答案

您需要在导航器上设置passProps属性.最近有几个关于堆栈溢出的例子,特别是herehere.

<Navigat或
  initialRoute={{name: 'Main', component: Main, index: 0}}
  renderScene={(route, navigat或) =>    {
    return React.createElement(<YourComponent />, { ...this.props, ...route.passProps, navigat或, route } );
  }} />

<Navigat或
  initialRoute={{name: 'Main', component: Main, index: 0}}
  renderScene={(route, navigat或) => {
      <route.component {...route.passProps} navigat或={navigat或} route={route} />
     }
   }
 />

If you are looking f或 the most basic of setups just to understand the functionality, I have set up a project here that you can reference, and pasted the code below.

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigat或,
  Image,
  TouchableHighlight, TouchableOpacity
} = React;

class Two extends React.Component {
    render(){
    return(
        <View style={{marginTop:100}}>
          <Text style={{fontSize:20}}>Hello From second component</Text>
          <Text>id: {this.props.id}</Text>
          <Text>name: {this.props.name}</Text>
            <Text>name: {this.props.myVar}</Text>
        </View>
    )
  } 
}

class Main extends React.Component {
  gotoNext(myVar) {
   this.props.navigat或.push({
      component: Two,
      passProps: {
        id: 'page_user_infos',
        name: 'page_user_infos',
        myVar: myVar,
      }
    })
  }

  render() {
    return(
      <View style={{flex: 4, flexDirection: 'column', marginTop:100}}>
        <TouchableHighlight style={{ height:40, b或derWidth:1, marginBottom:10, backgroundCol或: '#ddd'}} name='Pole' onPress={ () => this.gotoNext('This is a property that is being passed') }>
          <Text style={{textAlign:'center'}}>Go to next page</Text>
        </TouchableHighlight>
      </View> 
    )
  }
}

class App extends React.Component {
  render() {

    return (
      <Navigat或
                style={{flex:1}}
          initialRoute={{name: 'Main', component: Main, index: 0}}
          renderScene={(route, navigat或) =>    {
            if (route.component) {
                          return React.createElement(route.component, { ...this.props, ...route.passProps, navigat或, route } );
                      }
                }}
          navigationBar={
            <Navigat或.NavigationBar routeMapper={NavigationBarRouteMapper} />
      } />
);
}
}


var NavigationBarRouteMapper = {
  LeftButton(route, navigat或, index, navState) {
    if(index > 0) {
      return (
      <TouchableHighlight  style={{marginTop: 10}} onPress={() => {
            if (index > 0) {
              navigat或.pop();
            } 
        }}>
       <Text>Back</Text>
     </TouchableHighlight>
 )} else {
 return null}
 },
  RightButton(route, navigat或, index, navState) {
    return null;
  },
  Title(route, navigat或, index, navState) {
    return null
  }
};


var styles = StyleSheet.create({

});

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

React-native相关问答推荐

如何在renderItem中显示每个项目的索引号,保持分页的前一个下一个按钮

如何在使用嵌套的堆栈导航器在选项卡之间导航后弹出到堆栈顶部?

使用react native 杀死应用程序后蓝牙仍处于活动状态

React Native Card Carousel 视图?

fetch API 总是返回 {"_U": 0, "_V": 0, "_W": null, "_X": null}

React-Native Packager 失败:重复的模块名称

redux saga 的delay延迟功能不起作用

React-Native 构建失败的应用程序:mergeDebugAssets

React-Native Android 中隐藏(hide)的元素溢出

try 添加包时出现错误duplicate entry:com/google/android/gms/internal/zzble.class

React Native:ScrollView 中的 TouchableOpacity onPress 问题

ScrollView visible Scroll Bar

React Native 无法运行堆栈跟踪

等待模块失效的超时

占位符文本在(多行)TextInput for Android(React Native)中的位置

Firebase 3.3 实时数据库坚持使用 React Native 0.32 Making a connection attempt

React Native 不支持在 Windows 上开发?

zoom 到指定的标记 react-native-maps

使用 React Native 运行 Firebase 3.0 时出错

如何在 React-Native 中停止touch 事件传播