【问题标题】:Navigating to a different page within if statement在 if 语句中导航到不同的页面
【发布时间】:2016-08-06 08:01:35
【问题描述】:

我正在使用 react-native-router-flux 构建一个 React Native 项目,但导航有点问题。基本上,我正在构建一个登录页面,该页面使用数据库检查电子邮件/密码,并在匹配时存储访问令牌。然后我想立即从登录页面导航到主页,但我不知道如何实现。似乎 {Actions.SceneKey} 仅在 onPress 时有效。这是我的登录页面代码:

'use strict'

import React, { Component } from 'react';
import { AsyncStorage, StyleSheet, Text, View, TouchableHighlight, AlertIOS,} from 'react-native';
import { Actions } from 'react-native-router-flux'
import t from 'tcomb-form-native'

import ViewContainer from '../components/ViewContainer';
import StatusBarBackground from '../components/StatusBarBackground';

var STORAGE_KEY = 'access_token';

var Form = t.form.Form;

var Person = t.struct({
  email: t.String,
  password: t.String
});

const options = {};

var LoginIndexScreen = React.createClass({

  async _onValueChange(item, selectedValue) {
    try {
      await AsyncStorage.setItem(item, selectedValue);
      {Actions.HomeScreen}
    } catch (error) {
      console.log('AsyncStorage error: ' + error.message);
    }
  },

  _userSignup() {
    var value = this.refs.form.getValue();
    if (value) { // if validation fails, value will be null
      fetch("https://EXAMPLE.herokuapp.com/oauth/token", {
        method: "POST",
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          email: value.email,
          password: value.password,
        })
      })
      .then(console.log(response))
      .then((response) => response.json())
      .then((responseData) => {
        this._onValueChange(STORAGE_KEY, responseData.id_token),
        AlertIOS.alert(
          "Signup Success!"
        )
      })
      .done();
    }
  },

_userLogin() {
    var value = this.refs.form.getValue();
        if (value) { // if validation fails, value will be null
          fetch("https://EXAMPLE.herokuapp.com/oauth/token", {
            method: "POST",
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({
              email: value.email,
              password: value.password,
            })
          })
          .then((response) => response.json())
          .then((responseData) => {
            if (responseData.access_token) {
                console.log(responseData)
                this._onValueChange(STORAGE_KEY, responseData.access_token)
                {Actions.HomeScreen}
            } else {
                AlertIOS.alert(
                    "Login failed due to: " + responseData.message
                )
            }
          })
          .done();
        }
},

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.row}>
          <Form
            ref="form"
            type={Person}
            options={options}
          />
        </View>
        <View style={styles.row}>
          <TouchableHighlight style={styles.button} onPress={this._userSignup} underlayColor='#99d9f4'>
            <Text style={styles.buttonText}>Signup</Text>
          </TouchableHighlight>
          <TouchableHighlight style={styles.button} onPress={this._userLogin} underlayColor='#99d9f4'>
            <Text style={styles.buttonText}>Login</Text>
          </TouchableHighlight>
        </View>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    justifyContent: 'center',
    marginTop: 50,
    padding: 20,
    backgroundColor: '#ffffff',
  },
  title: {
    fontSize: 30,
    alignSelf: 'center',
    marginBottom: 30
  },
  buttonText: {
    fontSize: 18,
    color: 'white',
    alignSelf: 'center'
  },
  button: {
    height: 36,
    backgroundColor: '#48BBEC',
    borderColor: '#48BBEC',
    borderWidth: 1,
    borderRadius: 8,
    marginBottom: 10,
    alignSelf: 'stretch',
    justifyContent: 'center'
  },
});

module.exports = LoginIndexScreen;

任何帮助将不胜感激

【问题讨论】:

    标签: javascript navigation react-native


    【解决方案1】:

    Actions.SceneKeyActions 的方法。所以你需要这样称呼它:Actions.SceneKey()Actions.HomeScreen()

    当您像 onPress={Actions.HomeScreen} 一样将方法放在 onPress 上时,您会将方法作为道具发送到 TouchableHighlight,当您按下按钮时会调用该方法。

    因此,您需要将 if 块中的方法调用为 Actions.HomeScreen()

    【讨论】:

    • 不幸的是,这对我不起作用。我收到一条错误消息,说 _reactNativeRouterFlux.Actions.HomeScreen 不是函数。
    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2018-08-23
    • 2011-03-05
    • 2016-11-17
    • 1970-01-01
    相关资源
    最近更新 更多