将参数传递给嵌套的TabNavigator

时间:2018-07-02 09:48:11

标签: javascript reactjs react-native react-navigation

我有StackNavigator,其中嵌套了TabNavigator,我导航到VisitUserPageNew,并在正在渲染的第一个屏幕UserPage中获取了参数,并且有用。但随后,我尝试将参数传递给嵌套的UserSection并返回undefined

这是导航电话:

this.props.navigation.navigate('VisitUserPageNew',{
  visitorProps: {
    visitorNickName:this.props.linkPublisherNickName,
    visitor:true
  }
})

这些是我的组件:

class UserPage extends Component {
    visitorProps = this.props.navigation.getParam('visitorProps')

    render() {
        console.log(this.visitorProps) // return the object I need
        return(
            <View style={styles.container}>
                <Text>Text</Text>
                <View  style={{width:'100%',height:'100%',flex:1}}>
                    <UserSection
                        visitorProps={this.visitorProps}
                    />
                </View>    
            </View> 
        )
    }
} 

export default class UserLinks extends Component {
    render() {
        console.log(this.props.visitorProps) // return undefined
        return <View/>;
    }
}

export default class UserBookmarks extends Component {
    render() {
        console.log(this.props.visitorProps) // return undefined
        return <View/>;
    }
}  

这是路线结构:

export const UserSection = createMaterialTopTabNavigator({
    UserFeed: {
        screen: UserLinks,
    },
    Saved: {
        screen: UserBookmarks,
    },
})

export const UserPageNew = StackNavigator({
    UserSectionPage: {
        screen: UserPage
    },
    UserSection: {
        screen: UserSection
    },
})

export const Home = StackNavigator({
    Main: {
        screen: Main
    }, 
    VisitUserPageNew: {
        screen: UserPageNew
    },
})

因此将参数传递到第一个屏幕效果很好。但是,当我想继续将这些相同的参数传递给嵌套的UserSection时,它将失败。我也尝试在visitorProps = this.props.navigation.getParam('visitorProps')UserBookmarks内部调用UserLinks,但结果是相同的。我无法获得这些参数。

0 个答案:

没有答案