rn 通讯录树状结构

官网,以及网上都有常见的Navigator的案例,基本上都是Navigator的直接使用,如果先需要展开一个三级子目录,到第三级,然后点击item跳转到另一个页面,那么Navigator怎么进行数据的传递呢?由于三级目录在同一个页面展示,经三元运算符或if条件判断,确定子级的显示与隐藏,因此在最底层通过this.props获取不到navigator,需要进行参数的逐级传递,具体见以下案例:
这里写代码片
首先创建一个Navigator,跳转到第一个界面AddressTree;

import React, {Component} from 'react';
import {AppRegistry, StyleSheet, Navigator,Text, View} from 'react-native';
import TelSearch from './TelSearch';
import TelPeopleView from './TelPeopleView';
export default class telNormal extends Component {
    constructor(props){
        super(props);
        this.renderScene = this.renderScene.bind(this);
    }
    render() {
        return (
        <Navigator
          style={{flex:1,height:44}}
          initialRoute={{
             component: AddressTree,
              name: 'AddressTree',
          }}
          configureScene={(route)=>{
          return Navigator.SceneConfigs.HorizontalSwipeJump;
          }}
         renderScene={this.renderScene}
       />
     )
    }
    renderScene(route,navigator) {
        let Component = route.component;
        return (
            <Component {...route.params}  navigator={navigator}/>
        );
    }
}
AppRegistry.registerComponent('telNormal', () => telNorma)
第一个界面显示情况 
这里写代码片 通过三元运算符判断是否显示其子级地接送假数据let faDate = require('./../data/tsconfig.json');
export default class AddressTree extends Component {
    constructor(props){
        super(props);
        this.state = {
            testData:'',
        }
    }
 openChange(data,i) {
        data.open = !data.open;
        this.setState({});
    }
    render() {
       let Datas = faDate.data;
       cellContainer = [];
       for (let i = 1; i<Datas.length;i++) {
      // Datas[0].id == Datas[i].pId ?
      //在第一项id open=true,判断pId=1的数据open是false or true   false显示pId=1的数据,true显示他的子级
       cellContainer.push(
           Datas[0].id == Datas[i].pId ?(Datas[i].open == true ? <**CityOpenView** key={Datas[i].name+i} data={[Datas[i],Datas,i]}/> :<TouchableOpacity key={Datas[i].name + i} style={[style.listTitle,{height:44}]}
  onPress={
      this.openChange.bind(this, Datas[i], i)
   }
>
<Image style={style.image_second}
    source={Datas[i].open ?require('../img/person.png'):                               require('../img/contacts_tree_second.png')}
>
</Image>
<Text style={{ marginLeft: 8,fontSize:15,}}>
    {Datas[i].name} {Datas[i].id} {Datas[i].pId}
</Text>
</TouchableOpacity>):null
)
}
return (
   false == Datas[0].open ?
        //加载头布局
     ( <View style={{flex:1,backgroundColor:'#f4f5f7'}}>
         <NavigationBar title={'通讯树状图'}                                                              leftButtonTitle={'返回'}      leftButtonIcon{require('../img/back.png')}/>
   <ScrollView
       style={{marginTop:15}}
       automaticallyAdjustContentInsets = {true}
       showsVerticalScrollIndicator = {true}>
       {touchable}
   </ScrollView>
   </View> ) :
   <View style={{flex:1,}}>
       <NavigationBar title={'通讯树状图'}
                      leftButtonTitle={'返回'}
                      leftButtonIcon={require('../img/back.png')}/>
   <ScrollView
       style={{marginTop:15}}
       automaticallyAdjustContentInsets = {true}
       showsVerticalScrollIndicator = {true}>
       {touchable}
       {cellContainer}
    </ScrollView>
</View>
);
}
}
新的界面
export default class CityOpenView extends Component {
    constructor(props) {
        super(props);
        this.state = {
            data: this.props.data,// 接受一个数组,0:当前序号的数据,1:整个json数组,2:编号
        }
        this.openChangeForth = this.openChangeForth.bind(this);
    }
openChange(data,nowId) {
        data.open = !data.open;
        this.setState({});
    }

    openChangeForth(data,nowId) {
        const {navigator} = this.props;//若不进行navigator的参数传递,则获取不到navigator,会出图片所示错误
        InteractionManager.runAfterInteractions(() => {
            navigator.push({
                component: **TelPeopleView**,//新界面
                name: 'TelPeopleView',
                param:{

                }
            });
        });
    }

在其子级CityOpenView中开始跳转到新的一个界面时,由于最开始时的Navigator没有传递下来,当点击CityOpenView中的其中一个item的时候会找不到navigator这个属性,错误图见下:

找不到navigatro这个属性,所以在AddressTree


Datas[0].id == Datas[i].pId ?(Datas[i].open == true ? <**CityOpenView** key={Datas[i].name+i} data={[Datas[i],Datas,i]}/> :<TouchableOpacity ...>
的时候需要进行navigatro参数的传递
<**CityOpenView**   ***{...this.props}*** key={Datas[i].name+i} data={[Datas[i],Datas,i]}/>

通过***{…this.props}*** 可以一级一级的向子级进行传递,在三级子目录的时候也可以进行正常的跳转了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值