从标题中的按钮导航到不同的屏幕

Kak*_*kar 23 reactjs react-native

我正在使用来自react-native 的新React-navigation.我的导航如下:

StackNavigator:

  1. TabNavigator // HomeNavigation
  2. TabNavigator // NotificationNavigation

完整代码:

const MainNavigation = StackNavigator({
    Home: {
        screen: HomeNavigation,
    },
    Notification: {
        screen: NotificationNavigation,
    }
});

const HomeNavigation = TabNavigator({
    AllPost: {
        screen: All,
    },
    ArticlePost: {
        screen: Article,
    },
    BusinessPost: {
        screen: Job,
    },
});

HomeNavigation.navigationOptions = {
    title: 'Home',
    header: {
        right: <SearchNotification/>
    },
};

class SearchNotification extends React.Component {
    goToNotification = () => {
        this.props.navigate('Notification');
    };

    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity>
                    <Icon name="md-search" style={styles.Icon}/>
                </TouchableOpacity>
                <TouchableOpacity style={styles.notificationWrapper} onPress={this.goToNotification}>
                    <Icon name="md-notifications" style={styles.Icon}/>
                    <Text style={styles.number}>3</Text>
                </TouchableOpacity>
            </View>
        )
    }
}

const NotificationNavigation = TabNavigator({
    Comment: {
        screen: NotificationComment,
    },
    Follow: {
        screen: NotificationFollow,
    }
});
Run Code Online (Sandbox Code Playgroud)

HomeNavigation有一个标题,标题有一个SearchNotification的正确组件.SearchNotification有一个图标,在印刷中我想去NotificatoinNavigation.

但是,如果我以这种方式更改HomeNavigation的标题,则SearchNotification不会显示在标题中.

HomeNavigation.navigationOptions = {
    title: 'Home',
    header: {
        tintColor: 'white',
        style: {
            backgroundColor: '#2ec76e',
        },
        right: ({navigate}) => <SearchNotification navigate={navigate}/>
    },
};
Run Code Online (Sandbox Code Playgroud)

如何从标题中的按钮导航到不同的屏幕?

Kak*_*kar 8

所以问题是(我认为),navigationOptions而不是使用navigations我必须使用navigate,并将其作为道具传递给孩子(即SearchNotification).

所以最终的代码如下所示:

HomeNavigation.navigationOptions = {
    title: 'Home',
    header: ({navigate}) => ({
        right: (
            <SearchNotification navigate={navigate}/>
        ),
    }),
};
Run Code Online (Sandbox Code Playgroud)

SearchNotification组件中:

export default class SearchNotification extends React.Component {
    goToNotification = () => {
        this.props.navigate('Notification');
    };

    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity>
                    <Icon name="md-search" style={styles.Icon}/>
                </TouchableOpacity>
                <TouchableOpacity style={styles.notificationWrapper}
                                  onPress={() => this.props.navigate('Notification')}>
                    <Icon name="md-notifications" style={styles.Icon}/>
                    <Text style={styles.number}>3</Text>
                </TouchableOpacity>
            </View>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)