简体   繁体   English

如何将 TextInput 的值存储到本地存储并在应用程序以本机反应启动时获取它们?

[英]How can I store the value of TextInput to local storage and get them when the app starts in react native?

I'm making a to-do list app.我正在制作一个待办事项列表应用程序。 I need to store the input value locally or somewhere so that I can show them in the app even if the app is opened after closing once.我需要在本地或某处存储输入值,以便即使在关闭一次后打开应用程序也可以在应用程序中显示它们。 Now when I'm closing the app and restarting all the previous values is being vanished.现在,当我关闭应用程序并重新启动时,所有以前的值都将消失。 But I want to keep the previous data and If new data is given that will also be stored if I don't delete that manually.但是我想保留以前的数据,如果提供了新数据,如果我不手动删除它,也会存储这些数据。 How can I solve this problem?我怎么解决这个问题? I'm using expo.我正在使用世博会。 Here is my code:这是我的代码:

import React, { useState } from 'react';
import { View, Text, StyleSheet, Button, FlatList, TouchableOpacity, TextInput, ScrollView } from 'react-native';
import { MaterialIcons } from '@expo/vector-icons'
import Line from './Line';

function App() {

  //storing data in array
  const [initialElements, changeEl] = useState([

  ]);

  const [value, setValue] = useState('')
  const [idx, incr] = useState(1);
  const [exampleState, setExampleState] = useState(initialElements);

  //add button
  const addElement = () => {
    if (value != '') {
      var newArray = [...initialElements, { id: idx, title: value }];
      incr(idx + 1);
      setExampleState(newArray);
      changeEl(newArray);

    }

  }

  //delete button
  const delElement = (id) => {

    let newArray = initialElements.filter(function (item) {
      return item.id !== id
    })
    setExampleState(newArray);
    changeEl(newArray);
  }


  //showing item
  const Item = ({ title, id }) => (
    <View style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}>
      <View style={{ borderWidth: 2, margin: 5, flex: 1, padding: 10, borderRadius: 10 }}>
        <TouchableOpacity onPress={() => { alert(title) }}>
          <Text >{title}</Text>
        </TouchableOpacity>
      </View>
      <TouchableOpacity onPress={() => delElement(id)}>
        <MaterialIcons name='delete' size={24} />
      </TouchableOpacity>
    </View>
  );

  //calling item for render
  const renderItem = ({ item }) => {
   
<Item title={item.title} id={item.id} />

  }
    


  return (    

    <View style={styles.container}>

      <View style={{ alignItems: 'center', marginBottom: 10 }}>
        <Text style={{ fontSize: 20, fontWeight: '500' }}>  To <Text style={{ color: 'skyblue', fontWeight: '900' }}>Do</Text> List</Text>
        <Line />
      </View>

      <View>
        <TextInput onChangeText={text => setValue(text)} placeholder="Enter to Todo" style={{ borderWidth: 2, borderRadius: 10, backgroundColor: 'skyblue', height: 40, paddingLeft: 10, borderColor: 'black' }}></TextInput>
      </View>


      <View style={{ alignItems: 'center' }}>
        <TouchableOpacity onPress={() => addElement()} style={{ marginTop: 10 }}>
          <View style={{ backgroundColor: 'black', width: 70, height: 40, justifyContent: 'center', borderRadius: 10 }}>
            <Text style={{ color: 'white', textAlign: 'center' }}>Add</Text>
          </View>
        </TouchableOpacity>
      </View>

      <FlatList
        style={{ borderWidth: 2, padding: 10, flex: 1, backgroundColor: '#EEDDD0', marginTop: 10 }}
        data={exampleState}
        renderItem={renderItem}
        keyExtractor={item => item.id.toString()
        }

      />
    </View>
  );
}



const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 20,
    margin: 5,
  }
})

export default App;

React Native provides a local storage option called AsyncStorage. React Native 提供了一个名为 AsyncStorage 的本地存储选项。 You might use asyncstorage to save the data locally on the device and get that data inside useEffect() hook on startup.您可以使用 asyncstorage 将数据本地保存在设备上,并在启动时在 useEffect() 挂钩中获取该数据。

You can find more about AsyncStorage here.您可以在此处找到有关 AsyncStorage 的更多信息。

Although this is deprecated and now community package " @react-native-async-storage/async-storage " is used.虽然这已被弃用,但现在使用社区包“@react-native-async-storage/async-storage”。 The implementation remains the same.实现保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM