2

我使用Facebook SDK創建了一個登錄按鈕。用戶登錄後,該應用將導航到第二個屏幕(NavigatorIOS)。從第二個屏幕中,用戶可以使用導航(後退按鈕)返回到登錄屏幕。反應本機 - 防止用戶回到登錄屏幕

如何防止用戶返回到登錄屏幕(如果他已經登錄)?

index.ios.js

import React, { Component } from 'react' 

import { 
    AppRegistry, 
    StyleSheet, 
    NavigatorIOS 
} from 'react-native' 

import LoginView from './src/login-view' 

class MyApp extends Component { 
    render() { 
    return (
     <Provider store={store}> 
     <NavigatorIOS 
      initialRoute={{ 
      component: LoginView, 
      title: 'MyApp', 
      index: 0 
      }} 
     /> 
     </Provider> 
    ); 
    } 
} 
AppRegistry.registerComponent('MyApp',() => MyApp); 

LoginView

import React, {Component} from 'react' 
import { 
    View, 
    Text, 
    StyleSheet, 
    TouchableHighlight, 
} from 'react-native' 

import CategoryView from './category-view' 

import {LoginButton, AccessToken, GraphRequest, GraphRequestManager} from 'react-native-fbsdk' 

class LoginView extends Component { 
    goToCategoryView =() => 
     this.props.navigator.push({ 
      title: 'Categories', 
      component: CategoryView, 
     }) 

    render(){ 
     return(
      <View style={styles.container}> 
       <LoginButton 
        readPermissions={['public_profile','email']} 
        onLoginFinished={ 
         (error, result) => { 
          if (error) { 
           console.log('login has error: ', result.error) 
          } else if (result.isCancelled) { 
           console.log('login is cancelled.') 
          } else { 
           AccessToken.getCurrentAccessToken().then((data) => {  
            this.goToCategoryView() 
           }) 
          } 
         } 
        } 
        onLogoutFinished={() => {console.log('logged out')}} /> 
      </View> 
     ) 
    } 
} 

export default LoginView 

回答

2

使用導航器,可以使用resetTo(startingRoute)方法重置堆棧並從您過去的路由中啓動一個新堆棧一個參數。這樣做會阻止你重新回到棧中。

如果我沒有誤解你的組件,你應該使用這樣的事情:

goToCategoryView =() => { 
    //Replace here push with resetTo 
    this.props.navigator.resetTo({ 
     title: 'Categories', 
     component: CategoryView, 
    }) 
} 

Facebook Docs

+0

感謝您的回覆。好戲,試過了。它工作正常。 – John

+0

很高興爲您服務!你願意投票嗎?謝謝 –

+0

關於該主題的最後一個問題 - 一旦用戶登錄,如果我刷新模擬器,它將返回到顯示Facebook繼續按鈕的登錄屏幕。是否可以防止刷新/重新加載時返回到該屏幕? – John

0

如何pop()方法的登錄屏幕關閉,然後再按下()在新的屏幕上,或嘗試更換()? [https://facebook.github.io/react-native/docs/navigator.html]

還有一件事,你應該檢查登錄屏幕,看看用戶是否登錄並顯示登出而不登錄。 (如果您正在使用redux來存儲用戶令牌,則可以檢查該令牌是否存在並且仍然有效)

+0

感謝您的輸入。我最後使用了resetTo(路由)。 – John

+0

沒問題。 resetTo聽起來更好:) – user1736525