如何createStackNavigator导出为阶级阵营本土?(How to export cre

2019-09-26 20:41发布

我读到这里 ,使用的createStackNavigator作为一个组件类是可能的,而我试图做到这一点,但我总是得到一个错误。

取而代之的是:

export default createStackNavigator({
    Messages
}, {
    defaultNavigationOptions: {
        gesturesEnabled: false
    }
})

我尝试这样做:

class MessagesStack extends React.Component {
    render() {
        const RouteConfigs = {
            Messages
        };

        const NavigatorConfigs = {
            defaultNavigationOptions: {
                gesturesEnabled: false
            }
        };

        const Stack = createStackNavigator(RouteConfigs, NavigatorConfigs);
        return <Stack />;
    }
}

export default MessagesStack;

但是,我总是得到这样的错误:

Invariant Violation: Invariant Violation: The navigation prop is missing for this navigator. In react-navigation 3 you must set up your app container directly. More info: https://reactnavigation.org/docs/en/app-containers.html

所以,我试图纠正它,加入导航道具,就像这样:

return <Stack navigation={this.props.navigation} />;

但后来我得到了一个新的错误

TypeError: TypeError: No "routes" found in navigation state. Did you try to pass the navigation prop of a React component to a Navigator child? See https://reactnavigation.org/docs/en/custom-navigators.html#navigator-navigation-prop

这可能吗? 主要的原因我想这样做,是为了能够@Inject和使用@Observer为mobx商店。 当我在店里去改变背景颜色这样,它会改变每一个具有储存注入,并观察堆栈。

编辑

当我从打电话MessagesStack:

export default createBottomTabNavigator({
    DayStack: {
        screen: DayStack,
        navigationOptions: {...}
    }
    MessagesStack: {
        screen: MessagesStack,
        navigationOptions: {...}
    }
    ... OtherStacks here...
}, {
    initialRouteName: 'DayStack',
});

Answer 1:

根据从错误中我的理解,我认为这个问题是不是与你的createStackNavigator实际的问题是,现在从反应导航版本3,你必须在createStackNavigator手动传递到createAppContainer,以便创建容器可用于主要成分反应本地渲染

import { createAppContainer, createStackNavigator } from 'react-navigation';
// you can also import from @react-navigation/native

const AppNavigator = createStackNavigator(...);

// you have to pass the app navigator into app container like this
const AppContainer = createAppContainer(AppNavigator);

// Now AppContainer is the main component for React to render

export default AppContainer;


文章来源: How to export createStackNavigator as class in React Native?