刷新应用程序后如何更新状态-React Native?

时间:2019-01-07 19:10:19

标签: javascript android reactjs react-native redux

我正在开发一个带有简单欢迎屏幕的登录屏幕, 但是我在刷新应用程序时仍然处于启动屏幕[ Indicator ]并没有分开的状态,但状态有些问题,  我需要在删除存储密钥时将状态加载为另一种状态,但它对我不起作用:L,您能帮我解决这个问题吗?

myScreens

首次登录后

after Login

刷新应用程序[双R键]-我需要将我移到“登录”表单

enter image description here

这是我的代码

/Login.js

import React, { Component } from "react";
import {
  StyleSheet,
  View,
  StatusBar,
  Text,
  TextInput,
  TouchableOpacity,
  ActivityIndicator,
  AsyncStorage
} from "react-native";
export default class Login extends Component {
  constructor() {
    super();
    this.state = {
      username: "",
      password: "",
      isLoad: false
    };
  }

  async componentWillMount() {
    await AsyncStorage.getItem("@myApp:username").then(username => {
      if (!username || username === "") {
        this.setState({
          isLoad: true
        });
      } else {
        this.setState({ username: username });
        this.props.navigation.navigate("Home");
      }
    });
  }

  _onLogin = () => {
    let { username, password } = this.state;

    if (username !== "" && password !== "") {
      AsyncStorage.setItem("@myApp:username", this.state.username).then(
        username => {
          this.setState({ username: username });
        }
      );
      this.props.navigation.navigate("Home");
    }
  };
  render() {
    if (!this.state.isLoad) {
      return (
         <View
      style={{ flex: 1, justifyContent: "center", alignItems: "center" }}
    >
      <ActivityIndicator size="large" color="red" />
    </View>
      );
    } else {
      return (
        <View style={styles.container}>
          <StatusBar backgroundColor="#333" barStyle="light-content" />
          <Text style={{ fontSize: 28, margin: 10 }}>Login</Text>
          <TextInput
            placeholder="Username"
            onChangeText={username => {
              this.setState({ username: username });
            }}
            value={this.state.username}
            autoCorrect={false}
            returnKeyType="next"
            style={{
              padding: 10,
              margin: 15,
              borderBottomColor: "#333",
              borderBottomWidth: 1,
              width: "80%"
            }}
            editable={true}
          />
          <TextInput
            placeholder="Password"
            onChangeText={password => {
              this.setState({ password: password });
            }}
            secureTextEntry={true}
            value={this.state.password}
            autoCorrect={false}
            returnKeyType="next"
            style={{
              padding: 10,
              margin: 15,
              borderBottomColor: "#333",
              borderBottomWidth: 1,
              width: "80%"
            }}
            editable={true}
          />

          <TouchableOpacity
            style={{
              margin: 20,
              padding: 10,
              width: "70%",
              backgroundColor: "#1a73e8"
            }}
            onPress={this._onLogin}
          >
            <Text style={{ color: "#FFF", textAlign: "center", fontSize: 18 }}>
              Log In
            </Text>
          </TouchableOpacity>
        </View>
      );
    }
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#fff"
  }
});

./ Home.js

import React, { Component } from "react";
import {
  StyleSheet,
  View,
  ImageBackground,
  Alert,
  Text,
  AsyncStorage,
  TouchableOpacity
} from "react-native";
export default class Home extends Component {
  constructor() {
    super();
    this.state = {
      name: ""
    };
  }
  async componentDidMount() {
    try {
      const storedValue = await AsyncStorage.getItem("@myApp:username");
      this.setState({ name: storedValue });
      if (storedValue == null) {
        this.props.navigation.navigate("Login");
      }
    } catch (error) {
      Alert.alert("Error", "There was an error.");
    }
  }
  _onLogout = () => {
    AsyncStorage.removeItem("@myApp:username").then(() => {
      this.setState({ name: "" });
    });
    this.props.navigation.navigate("Login");
  };
  render() {
    return (
      <View style={styles.container}>
        <ImageBackground
          source={require("./assets/bg1.jpg")}
          style={{
            flex: 1,
            width: "100%",
            height: "100%"
          }}
        >
          <View
            style={{
              backgroundColor: "rgba(0,0,0,0)",
              flex: 1,
              justifyContent: "center"
            }}
          >
            <Text
              style={{
                alignSelf: "center",
                backgroundColor: "#000fff",
                padding: 10,
                color: "#fff"
              }}
            >
              {`Hello, ${this.state.name} `}
            </Text>
            <TouchableOpacity
              style={{
                margin: 20,
                padding: 10,
                width: "50%",
                backgroundColor: "#1a73e8",
                justifyContent: "center",
                alignSelf: "center"
              }}
              onPress={this._onLogout}
            >
              <Text
                style={{ color: "#FFF", textAlign: "center", fontSize: 18 }}
              >
                Log out?
              </Text>
            </TouchableOpacity>
          </View>
        </ImageBackground>
      </View>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

问题出在您的componentWillMount

  async componentWillMount() {
    await AsyncStorage.getItem("@myApp:username").then(username => {
      if (!username || username === "") {
        this.setState({
          isLoad: true // This is the issue
        });
      } else {
        this.setState({ username: username });
        this.props.navigation.navigate("Home");
      }
    });
  }

首先,应避免使用componentWillMount并使用componentDidMount,因为现已弃用前者,不建议在较新版本的react中使用。

问题是,注销后导航到主屏幕时,正在检查username中的AsyncStorage,但是如果未定义,则设置为isLoad状态为true,则呈现ActivityIndicator。如果有任何承诺,您应该先进行isLoad处理。如果您将代码替换为:

 componentDidMount() {
   this.setState({ isLoad: true });
   AsyncStorage.getItem("@myApp:username").then(username => {
      if (!username || username === "") {
        this.setState({
          isLoad: false // change this to False
        });
      } else {
        this.setState({ username: username, isLoad: false });
        this.props.navigation.navigate("Home");
      }
    });
  }

您应该能够看到登录屏幕而不是微调框。希望这会有所帮助。