Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从具有2个属性的异步存储中获取密钥_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 从具有2个属性的异步存储中获取密钥

Javascript 从具有2个属性的异步存储中获取密钥,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我是个新来的本地人。在调用TextInput时,我尝试使用两个属性myKey和costKey获取“Key”和“Key2”。 2个文本输入值保存在异步存储键中。现在我尝试用两个不同的属性来调用它们,它们是“myKey”和“costKey”。 请建议,如何在呼叫时获取两个具有2个属性的已保存密钥 //AddScreen.js import React, { Component } from 'react'; import { AppRegistry, AsyncStorage, View, Text

我是个新来的本地人。在调用TextInput时,我尝试使用两个属性myKey和costKey获取“Key”和“Key2”。 2个文本输入值保存在异步存储键中。现在我尝试用两个不同的属性来调用它们,它们是“myKey”和“costKey”。 请建议,如何在呼叫时获取两个具有2个属性的已保存密钥

//AddScreen.js
import React, { Component } from 'react';
import { AppRegistry, AsyncStorage, View, Text, Button, TextInput, StyleSheet, Image, TouchableHighlight, Linking } from 'react-native';
import styles from '../components/styles';
import { createStackNavigator } from 'react-navigation';
import History from '../components/History';

    export default class AddScreen extends Component {
        constructor(props) {
            super(props);
            this.state = {
                myKey: '',
                costKey: '',
                text1: '',
                text2: '',
            }
        }
        async getKey() {
            try {
                const value = await AsyncStorage.getItem('@MySuperStore:key');
                const key = await AsyncStorage.getItem('@MySuperStore:key');

                const key1 = await AsyncStorage.getItem('@MySuperStore:key1');
                const key2 = await AsyncStorage.getItem('@MySuperStore:key2');
                this.setState({ myKey: key }, { costKey: key2 });
            } catch (error) {
                console.log("Error retrieving data" + error);
            }
        }

        async saveKey(text1, text2) {
            key = text1 + text2;
            try {
                await AsyncStorage.setItem('@MySuperStore:key', key);
                await AsyncStorage.setItem('@MySuperStore:key1', text1);
                await AsyncStorage.setItem('@MySuperStore:key2', text2);
            } catch (error) {
                console.log("Error saving data" + error);
            }
        }

        async resetKey() {
            try {
                await AsyncStorage.removeItem('@MySuperStore:key');
                const value = await AsyncStorage.getItem('@MySuperStore:key');
                this.setState({ myKey: value }, { costKey: value });
            } catch (error) {
                console.log("Error resetting data" + error);
            }
        }

    render() {
            const { navigate } = this.props.navigation;
            return (
                <View style={styles.MainContainer}>

                    <TextInput
                        style={styles.formInput}
                        placeholder="Enter key you want to save!"
                        value={this.state.myKey}
                        onChangeText={(value) => this.setState({ text1: value })}
                    />
                    <TextInput
                        style={styles.formInput}
                        placeholder="Enter key you want to save!"
                        value={this.state.costKey}
                        onChangeText={(value) => this.setState({ text2: value })}/>
                    <Button
                        onPress={() => this.saveKey(this.state.text1, this.state.text2)}
                        title="Save key"
                    />
                    <Button
                        style={styles.formButton}
                        onPress={this.getKey.bind(this)}
                        title="Get Key"
                        color="#2196f3"
                        accessibilityLabel="Get Key"
                    />
                    <Button
                        style={styles.formButton}
                        onPress={this.resetKey.bind(this)}
                        title="Reset"
                        color="#f44336"
                        accessibilityLabel="Reset"
                    />

                    <Text style={styles.instructions}>
                        Stored key is = {this.state.myKey}
                    </Text>
                    <Text style={styles.instructions}>
                        Stored key is = {this.state.costKey}
                    </Text>
                </View>
            )
        }
    }
//AddScreen.js
从“React”导入React,{Component};
从“react native”导入{AppRegistry、AsyncStorage、View、Text、Button、TextInput、样式表、图像、TouchableHighlight、Linking};
从“../components/styles”导入样式;
从“反应导航”导入{createStackNavigator};
从“../components/History”导入历史记录;
导出默认类AddScreen扩展组件{
建造师(道具){
超级(道具);
此.state={
myKey:“”,
成本键:'',
文本1:“”,
文本2:“”,
}
}
异步getKey(){
试一试{
const value=wait AsyncStorage.getItem('@MySuperStore:key');
const key=wait AsyncStorage.getItem('@MySuperStore:key');
const key1=await AsyncStorage.getItem('@MySuperStore:key1');
const key2=await AsyncStorage.getItem('@MySuperStore:key2');
setState({myKey:key},{costKey:key2});
}捕获(错误){
console.log(“检索数据时出错”+错误);
}
}
异步保存键(text1、text2){
按键=text1+text2;
试一试{
等待AsyncStorage.setItem('@MySuperStore:key',key);
等待AsyncStorage.setItem('@MySuperStore:key1',text1);
等待AsyncStorage.setItem('@MySuperStore:key2',text2);
}捕获(错误){
console.log(“保存数据错误”+错误);
}
}
异步重置键(){
试一试{
等待AsyncStorage.removeItem('@MySuperStore:key');
const value=wait AsyncStorage.getItem('@MySuperStore:key');
this.setState({myKey:value},{costKey:value});
}捕获(错误){
console.log(“错误重置数据”+错误);
}
}
render(){
const{navigate}=this.props.navigation;
返回(
this.setState({text1:value})}
/>
this.setState({text2:value})}/>
this.saveKey(this.state.text1,this.state.text2)}
title=“保存密钥”
/>
存储的密钥为={this.state.myKey}
存储的密钥为={this.state.costKey}
)
}
}

请举我的例子,说明如何分别调用两个不同的属性。

您的请求不太清楚,但在一个简短的分析中,我注意到您使用的
setState()
不正确(在
getKey()
resetKey()
)。您将状态声明为一个带有一些键的对象,因此您应该通过一个具有相同结构的新对象对其进行修改:

this.setState({
  ...this.state,
  myKey: key,
  costKey: key2
});