android - React Native - 如何从 componentWillReceiveProps 重定向到登录 View

标签 android ios reactjs react-native

我正在尝试检测用户是否在应用程序启动期间登录,如果未登录,则将他重定向到登录屏幕。

import * as AuthView from '../modules/auth/AuthView';    
const AppView = React.createClass({
    propTypes: {
        isReady: PropTypes.bool.isRequired,
        isLoggedIn: PropTypes.bool.isRequired,
        dispatch: PropTypes.func.isRequired
    },
    componentDidMount() {
        snapshotUtil.resetSnapshot()
            .then(snapshot => {
                const {dispatch} = this.props;

                if (snapshot) {
                    dispatch(SessionStateActions.resetSessionStateFromSnapshot(snapshot));
                } else {
                    dispatch(SessionStateActions.initializeSessionState());
                }

                store.subscribe(() => {
                    snapshotUtil.saveSnapshot(store.getState());
                });
            });
    },

    componentWillReceiveProps({isReady, isLoggedIn}) {
        if (!this.props.isReady) {
            if (isReady && !isLoggedIn) {
                console.warn("Not logged in, trying to show login screen here...");
               AuthView.showLoginView();
            }
        }
    },

    render() {
        if (!this.props.isReady) {
            return (
                <View>
                    <ActivityIndicator style={styles.centered}/>
                </View>
            );
        }
        return (
            <View style={{flex: 1}}>
                <NavigationViewContainer />
            </View>
        );
    }
});

我的 AuthView 看起来像这样:

export function showLoginView() {
    var loginView = <View>
        <Text>
            This is a Login View
        </Text>
    </View>;

    return loginView;
}

输出为空白。看起来像这样:http://i.imgur.com/6CgvOGW.png

因此它显示警告消息 (console.warn) 并显示“ActivityIndi​​cator”。但不是登录面板。

我的问题是,如何修改 showLoginView() 函数以显示全屏登录 View ?

最佳答案

您必须在 render() 函数中显示所有 View 。如果您开发复杂的应用程序,请考虑使用 Navigator 组件。 在你的情况下:

 componentWillReceiveProps({isReady, isLoggedIn}) {
    if (!this.props.isReady) {
        if (isReady && !isLoggedIn) {
            console.warn("Not logged in, trying to show login screen here...");
           this.state.showLogin = true;
        }
    }
 },
 render() {
    if (this.state.showLogin) {
        return showLoginView()
    } 
    if (!this.props.isReady) {
        return (
            <View>
                <ActivityIndicator style={styles.centered}/>

...

关于android - React Native - 如何从 componentWillReceiveProps 重定向到登录 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187647/

相关文章:

android - 如何使用 EditTextPreference 作为屏蔽密码文本字段?

android - Bundle.getString(String key) 上的默认值

Android - 如何从多个 SQLite 表中检索数据?

ios - AVAsset 不适用于本地文件

iphone - 将音乐从 iPhone 流式传输到 iPhone 的最佳途径

javascript - 根据旧状态和 promise 结果计算新状态

没有 Activity 的 Android 应用程序将充当服务并在设备启动时启动?

iphone - 在 drawRect : of UIView 之外的 iOS 中为 CGLayer 创建窗口图形上下文

html - 如何创建文本居中的梯形 div?

javascript - react 未捕获错误 : Target container is not a DOM element