动态链接Firebase在带有2个导航器的应用程序中不起作用

时间:2019-07-05 04:17:36

标签: android react-native react-navigation firebase-dynamic-links

我是新手,本机。我对Dynamic Link Firebase有问题。

如果将initRouterName中的BottomTabNavigatgor设置为Author,则我将动态链接称为BookDetailScreen,它将转到正确的BookDetailScreen

但是,如果我将initRouterName中的BottomTabNavigator设置为Product,则我将动态链接称为BookDetailScreen,它将转到ProductScreen

有什么办法解决这个问题? 这是我的代码:

Router.js

const ProductNavigator = createStackNavigator({
    Product: {
        screen: ProductScreen
    },
    ProductDetail: {
        screen: ProductDetailScreen,
    }
}, {
    initialRouteName: 'Product'
});

const AuthorNavigator = createStackNavigator({
    Author: {
        screen: AuthorScreen
    },
    Book: {
        screen: BookScreen
    },
    BookDetail: {
        screen: BookDetailScreen
    }
}, {
    initialRouteName: 'Author'
});

const AppNavigator = createBottomTabNavigator({
    Author: {
        screen: AuthorNavigator
    },
    Product: {
        screen: ProductNavigator
    }
}, {
    initialRouteName: 'Product'
});


const AppContainer = createAppContainer(AppNavigator);

const MainApp = () => <AppContainer />

export default MainApp;

AuthorScreen.js

export default class AuthorScreen extends Component {
    static navigationOptions = {
        title: 'Author'
    };

    componentDidMount() {
        firebase.links().getInitialLink()
        .then(url => {
            if (url) {
                this.navigate(url);
            } else {
                console.log('URL null');
            }
        })
        .catch(error => console.error(error));
    }

    navigate = url => {
        const {navigate} = this.props.navigation;
        const route = url.replace(/.*?:\/\//g, '');
        const routeName = route.split('/')[0];
        const authorId = route.split('/')[1];
        const bookId = route.split('/')[2];

        if (routeName === 'BookDetail') {
            for (author of authors) {
                if (author.id === parseInt(authorId)) {
                    for (book of author.books) {
                        if (book.id === bookId.toString()) {
                            navigate(routeName, {
                                content: book.content
                            });
                        }
                    }
                }
            }
        }
    }

    render() {
        return (
            <View style={{flex: 1, justifyContent: 'center', alignContent: 'center'}}>
                <FlatList
                    data={authors}
                    renderItem={({item}) => {
                        return (
                            <TouchableOpacity
                                onPress={() => this.props.navigation.navigate('Book', {
                                    books: item.books,
                                })}
                            >
                                <Text>{item.name}</Text>
                            </TouchableOpacity>
                        );
                    }}
                    keyExtractor={item => item.id.toString()}
                />
            </View>
        )
    }
}

0 个答案:

没有答案