ReactNative Demo - Navigator 使用

  import React, {
    Component
  } from 'react';
  import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    DrawerLayoutAndroid,
    ListView,
    PixelRatio,
    Navigator,
    ScrollView,
  } from 'react-native';

  const Header = require("./Header")

  class AwesomeProject extends Component {


    render() {

      let defaultName='List';
      let defaultComponent=List;

      return (
          <Navigator 
            initialRoute={{name: defaultName, component: defaultComponent}}
            configureScene={
              (route) => 
              {
                return Navigator.SceneConfigs.HorizontalSwipeJumpFromRight;
              }
            }
            
            renderScene={
              (route,navigator)=>
              {
                let Component = route.component;
                return <Component {...route.params} navigator={navigator}/>
              }
            }/>
      
      );


    }
  }



 class List extends Component{
   constructor(props) {
     super(props);
     this.state = {author:"zcw",id:1,user:null,};
   }
   
   _pressButton(){
     const {navigator} = this.props;
     const self = this;
     if(navigator) {
       navigator.push({
         name : 'Detail',
         component : Detail,
         params : {
           author: this.state.author,
           id:this.state.id,
           getUser:function(user) {
             self.setState({
                user:user
             })
           }
         }
       })
     }
   }
   
   render(){
        if(this.state.user) {
           return(
              <ScrollView style={styles.flex}>
                  <Text style={styles.list_item} onPress={this._pressButton.bind(this)}>用户信息:{JSON.stringify(this.state.user)}</Text>
             </ScrollView>
           );
        }else {
          return(
          
              <ScrollView style={styles.flex}>
                <Text style={styles.list_item} onPress={this._pressButton.bind(this)}>* 豪华游轮济州岛3日游</Text>
                <Text style={styles.list_item} onPress={this._pressButton.bind(this)}>* 豪华游轮台湾3日游</Text>
                <Text style={styles.list_item} onPress={this._pressButton.bind(this)}>* 豪华游轮塔克拉玛干岛3日游</Text>

              </ScrollView>
          
            );
        }
    }
 }
 
 class Detail extends Component{
   constructor(props) {
     super(props);
     this.state={};
   }
   
   componentDidMount(){
     this.setState({
       author:this.props.author,
     });
   }
   
   _pressButton (){
     const {navigator} = this.props;
     if(this.props.getUser) {
       let user = MODEL_USERS[this.props.id];
       this.props.getUser(user);
     }
     if(navigator) {
       navigator.pop();
     }
   }
   
   render(){
     return(
       <ScrollView>
          <Text style={styles.list_item} onPress={this._pressButton.bind(this)}>作者是:{this.state.author}</Text>
       </ScrollView>
     );
   }
 }

 
 
 

  const styles = StyleSheet.create({
    flex: {
      flex:1,
    },

    
    news_item:{
      marginLeft:10,
      marginRight:10,
      justifyContent:'center',
      height:40,
    },

    news_title_font:{
      fontSize:20,
      color:'red',
    },

    list_item_font:{
      fontSize:16,

    },
    list_item:{
      marginLeft:10,
      marginRight:10,
      justifyContent:'center',
      height:40,
      borderBottomWidth:1/PixelRatio.get(),
      borderBottomColor: '#ddd',
    },
  
    
  });
  
  const MODEL_USERS={
    1:{name:'mot',age:23},
    2:{name:'zcw',age:22}
  };

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

转载请注明出处://www.greatytc.com/u/c864df301e25

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,839评论 25 709
  • 前些日子从@张鑫旭微博处得一份推荐(Front-end-tutorial),号称最全的资源教程-前端涉及的所有知识...
    谷子多阅读 9,775评论 0 44
  • 远处的山模模糊糊看得见了,近处的小山、树林、江面渐渐明晰,还能隐隐约约地看见几只小渔船漂在江面上。忽然,一道亮光刺...
    京师久月阅读 929评论 0 0
  • 独自一人走路的时候,我常常会想,人这一生最需要的能力是什么呢,又或者人可以依靠一种什么样的心态来把自己的一生过的欢...
    瑾初阅读 845评论 0 0
  • 上周五下午从公司离职踏出大门的那刻起,心情怪怪的,没有因为不用工作了而高兴,也没有因为离开了太伤悲,因为两...
    FE_Girl阅读 5,674评论 0 4