我如何在 class 方法中使用 react-navigation?
How can i use react-navigation in class method?
import * as React from 'react';
import { createAppContainer, createSwitchNavigator } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { Text, View } from 'react-native';
class LoadingScreen extends React.Component {
dataCheck = () => {
for (i = 0; i < 5; i++) { // data loading time in real code
if (i == 4) {
this.props.navigation.navigate('AppStack')
}
}
}
render() {
return (
<View>
<Text>loading</Text>
{this.dataCheck}
</View>
);
}
}
class MainScreen extends React.Component {
render() {
return (
<View>
<Text>mainscreen</Text>
</View>
);
}
}
const AppNavigator = createStackNavigator(
{
main: MainScreen
},
{
initialRouteName: 'main'
}
)
const LoadNavigator = createSwitchNavigator(
{
load: LoadingScreen,
AppStack: AppNavigator
},
{
initialRouteName: 'load'
}
)
export default createAppContainer(LoadNavigator)
我试图在加载数据时显示加载屏幕。所以我在 class 'LoadingScreen' 中编写了方法 'dataCheck',它等待数据加载(对于上面代码中的语句)并导航到 'AppStack'。 但我认为“this.props.navigation.navigate('AppStack')”行不通。如何在 class 的方法中使用 react-navigation?
你只需要更换
{this.dataCheck}
有
{this.dataCheck()}