値がtrueの場合、AsyncStorage
からキーの値を取得するindex.android.jsがあり、DashboardScreen
に移動し、StackNav
に移動したいとします。条件ベースでコンポーネントをレンダリング/ナビゲートする方法
index.android.js
/**
* @flow
*/
import React, { Component } from "react";
import { AppRegistry } from "react-native";
import { StackNav } from "./router";
export default class ScheduledApp extends Component {
render() {
const { navigate } = this.props.navigation;
try {
AsyncStorage.getItem("@ProductTour:key").then(value => {
// control goes inside if when user has completed product tour
if (value) {
navigate("DashboardScreen");
}
});
} catch (error) {
console.log(error);
}
return <StackNav />;
}
}
AppRegistry.registerComponent("Scheduled",() => ScheduledApp);
router.js
import { StackNavigator } from "react-navigation";
import SplashScreen from "./ui/SplashScreen";
import EnableNotificationScreen from "./ui/EnableNotificationScreen";
import CreateMessageScreen from "./ui/CreateMessageScreen";
import DashboardScreen from "./ui/DashboardScreen";
import CreateSMS from "./ui/CreateSMS";
export const StackNav = StackNavigator(
{
Splash: {
screen: SplashScreen,
navigationOptions: {
header: null
}
},
EnableNotification: {
screen: EnableNotificationScreen,
navigationOptions: {
header: null
}
},
CreateMessage: {
screen: CreateMessageScreen,
navigationOptions: {
header: null
}
},
DashboardScreen: {
screen: DashboardScreen,
navigationOptions: {
headerLeft: null,
headerTitleStyle: { alignSelf: "center" }
}
},
CreateSMS: {
screen: CreateSMS
}
},
{
initialRouteName: "Splash"
}
);
誰が/レンダリング条件ごとにコンポーネントを移動する方法を知っていますか?
あなたのエラーは、あなたのコードと一致していないようです。このエラーでは、非構造化代入はレンダリング関数の外にあります。その場合、無効な構文になります。しかし、あなたのコードはrender関数の中にあるようです。 –
@CarlosCに同意し、 'this.props.navigation'はあなたの' ScheduledApp'コンポーネントにまだ公開されていません。 –
@CarlosC正しい、間違いなし。値が真であればレンダリングする方法を知っていますか?それから 'DashboardScreen'と' StackNav.'にナビゲートしたいのですか? –