1. 程式人生 > >RN身份驗證與資料儲存

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”); }} /> )} {this.state.data && ( <TextInput onChangeText={dd => { this.setState({ aa: dd }); }} /> <Button title=“新增” onPress={async () => { await AsyncStorage.setItem(“dd”, this.state.aa); }} /> <Button title=“刪除” onPress={async () => { await AsyncStorage.removeItem(“dd”); }} /> <Button title=“查詢” onPress={async () => { const ddd = await AsyncStorage.getItem(“dd”); alert(ddd); }} /> )} ); } } 、、、、、、、、、、、、、、、登陸頁面 import React, { Component } from “react”; import { View, Text, AsyncStorage, TextInput, Button } from “react-native”;

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”,

this.state.name); this.props.navigation.navigate(“Wode”); }} /> ); } }