当我尝试启动我的本机应用程序时,我收到此消息。通常这种格式适用于其他多屏幕导航,但在某种程度上不适用于这种情况。
这是错误:
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
这是我的应用格式:
import React, {Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { createStackNavigator } from 'react-navigation';
import Login from './view/login.js'
import SignUp from './view/signup.js'
const RootStack = createStackNavigator(
{
Home: {
screen: Login
},
Signup: {
screen: SignUp
}
},
{
initialRouteName: 'Home'
}
);
export default class App extends React.Component {
render() {
return <RootStack />;
}
}
React Navigation 3.0有许多breaking changes,包括根导航器所需的显式应用程序容器。
过去,任何导航器都可以充当应用程序顶层的导航容器,因为它们都包含在“导航容器”中。导航容器(现在称为app容器)是一个高阶组件,它维护应用程序的导航状态并处理与外部世界的交互,以将链接事件转换为导航操作等。
在v2及更早版本中,React Navigation中的容器由create * Navigator函数自动提供。从v3开始,您需要直接使用容器。在v3中,我们还将createNavigationContainer重命名为createAppContainer。
import {
createStackNavigator,
createAppContainer
} from 'react-navigation';
const MainNavigator = createStackNavigator({...});
const App = createAppContainer(MainNavigator);
一个更全面的代码示例:
import {
createStackNavigator,
createAppContainer
} from 'react-navigation';
import Login from './view/login.js'
import SignUp from './view/signup.js'
const RootStack = createStackNavigator({
Home: {
screen: Login
},
Signup: {
screen: SignUp
}
});
const App = createAppContainer(RootStack);
export default App;
@Tom Dickson是这样的:
import React, { Component } from 'react';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import ScreenOne from './ScreenOne';
import ScreenTwo from './ScreenTwo';
const NavStack = createStackNavigator({
ScreenOne: {
screen: ScreenOne,
},
ScreenTwo: {
screen: ScreenTwo,
},
});
const App = createAppContainer(NavStack);
export default App;
然后用它来引用它
<App />
创建一个新文件ScreenContainer.js(您可以选择名称)。然后在ScreenContainer文件中添加:
import React, { Component } from 'react';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import MainScreen from './MainScreen';
import AboutScreen from './AboutScreen';
const NavigationStack = createStackNavigator({
Main: {
screen: MainScreen,
},
About: {
screen: AboutScreen,
},
});
const Container = createAppContainer(NavigationStack);
export default Container;
然后在你的App.js文件中:
import Container from './ScreenContainer';
class App extends Component {
render() {
return (
<Container />
);
}
}
这是另一种方式
import React, {Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { createStackNavigator,createAppContainer } from 'react-navigation';
import Login from './view/login.js'
import SignUp from './view/signup.js'
const RootStack = createStackNavigator(
{
Home: {
screen: Login
},
Signup: {
screen: SignUp
}
},
{
initialRouteName: 'Home'
}
);
class App extends React.Component {
render() {
return <RootStack />;
}
}
export default createAppContainer(RootStack);
import React, { Component } from 'react';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import Home from './home';
import Details from './details';
const Root = createStackNavigator({
home: {
screen: Home,
},
details: {
screen: Details,
},
});
const container = createAppContainer(Root);
export default container;
在你的App.js文件中用</container>
引用它
我的代码位于底部
export default class App extends React.Component {
render() {
return (
<View >
<SimpleApp style={{ width: Dimensions.get("window").width }} />
</View>
);
}
}
我只是用它代替它,它就像一个魅力。当然,这是因为反应导航库中的更新:
const App = createAppContainer(SimpleApp);
export default App;
此外,我还将createAppContainer库包含在顶部的react-navigation中。
这个是创建一个带有两个标签的底部导航器:
import {createBottomTabNavigator, createAppContainer} from 'react-navigation';
export class Home extends Component{
//...
}
export class Settings extends Component{
//...
}
const navig = createBottomTabNavigator({
Home: Home,
Settings: Settings
});
const App = createAppContainer(navig);
export default App;