【问题标题】:How AsyncStorage should be modified such that it should persist inputtext value after refresh as well?应该如何修改 AsyncStorage 以使其在刷新后也保持 inputtext 值?
【发布时间】:2020-11-11 22:54:54
【问题描述】:

我的代码在 textinput 中输入并保存在 asynchStorage 中,这样可以正常工作。我需要一些帮助来修改我的代码,以便在刷新后 textinput 应该填充用户使用 redux-persist 输入的相同文本。这是我的 App.js

import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, TextInput, Button, View, AsyncStorage} from 'react-native';

export default class AsyncStorageExample extends Component {

  constructor(props) {
    super(props);
    this.state = {
        myKey: null
    }
  }

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

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

  render() {
    return (
      <View>
        <TextInput
          style={styles.formInput}
          placeholder="Enter key you want to save!"
          value={this.state.myKey}
          onChangeText={(value) => this.saveKey(value)}
          />
      </View>
    );
  }
}
AppRegistry.registerComponent('AsyncStorageExample', () => AsyncStorageExample)

【问题讨论】:

    标签: reactjs react-native persistent-storage


    【解决方案1】:

    您可以在启动画面中调用 getKey() 或 View 的 ComponentDidMount() 方法来显示文本输入

    【讨论】:

    • 这就是我要问的怎么做?
    • 有刷新 textInput 的功能吗?或者什么时候刷新?
    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多