RN身份驗證與資料儲存
import React, { Component } from “react”; import { View, Text, AsyncStorage, Button, Switch, TextInput } from “react-native”; import SwithNav from “./SwithNav”;
export default class Wode extends Component { constructor(props) { super(props); this.state = { name: “”, denlung: false, data: false, aa: “” }; } componentDidMount() { this.Shifou(); } Shifou = async () => { const namem = await AsyncStorage.getItem(“name”); if (namem) { this.setState({ name: namem, denlung: true }); } else { this.setState({ name: namem, denlung: false }); } };
getdata = dd => {
this.setState({
data: dd
});
};
render() {
return (
{this.state.denlung ? (
<Button
title=“登出”
onPress={async () => {
await AsyncStorage.setItem(“name”, this.state.name);
this.props.navigation.navigate(“Login”);
}}
/>
) : (
<Button
title=“請登入”
onPress={() => {
this.props.navigation.navigate(“Login”);
}}
/>
)}
export default class Login extends Component { constructor(props) { super(props); this.state = { name: “”, denlung: false }; } change = text => { this.setState({ name: text, denlung: true }); };
render() {
return (
<Button
title=“提交”
onPress={async () => {
await AsyncStorage.setItem(“name”,