ReactNative

React Native之Navigator使用,參數的順傳和逆

2017-10-10  本文已影响0人  IPFK
render() {
        return (
            <Navigator
                initialRoute={{
                    component:FirstView,
                    params:{
                      title:'第一个视图',
                        haha:'哈哈',
                        hehe:'呵呵',
                        heihei:'嘿嘿'
                    }

                }}
                renderScene={(route, navigator) =>
                    <route.component {...route.params} nav={navigator}  />}
                configureScene={()=>//跳轉動畫
                    Navigator.SceneConfigs.FloatFromBottom
                }
            />
        );
    }

可以簡單地理解為RN中的Navigator就是route!!!

兩個最基本屬性分別為:

1,initialRoute

2,renderScene

下面為FirstView中的代碼

render() {
        return (
           <View style={styles.container}>
               {/*导航条*/}
               <View style={styles.navStyle}>
                   {/*导航条中间的文字*/}//後面的名字是驗證參數回傳的!!!
                   <Text>{this.props.title+'名字是'+this.state.userName}</Text>
               </View>
//-----
               <TouchableOpacity
                onPress={()=>this.viewClick()}
               >
                    <Text>{this.props.title}</Text>//通過props拿到傳遞來的title

               </TouchableOpacity>
           </View>
        );
    }

FirstView的文字點擊方法

viewClick(){
        //props 属性!!! 这个属性是你这个对象创建的时候定义的!!!
        this.props.nav.push({
            component:SecondView,
            params:{
               title:'这是第二个界面',
                //从第二个页面获取userName
                getUserName:function (user) {
                    this.setState({
                        userName:user
                    })
                }.bind(this)
            }
        })
    }

其中的getUserName

//---

    viewClick(){
        //为了严谨起见!!先判断
        if(this.props.getUserName){
            this.props.getUserName('我是逆傳過來的值!!')
        }

        //props 属性!!! 这个属性是你这个对象创建的时候定义的!!!
        this.props.nav.pop();
    }

在SecondView的點擊事件將要POP返回到FirstView時先調用回調函數就可以回傳值給FirstView了!!!

上一篇 下一篇

猜你喜欢

热点阅读