Persist
AsyncStorage 설치
expo install @react-native-async-storage/async-storage
AsyncStorage 사용
import AsyncStorage from "@react-native-async-storage/async-storage";
...
const saveToDos = async (toSave) => {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(toSave));
};
const loadToDos = async () => {
const s = await AsyncStorage.getItem(STORAGE_KEY);
s !== null ? setToDos(JSON.parse(s)) : null;
};
https://react-native-async-storage.github.io/async-storage/docs/usage/
사용법 참조하기
전체 소스코드
(App.js)
import { StatusBar } from "expo-status-bar";
import { useEffect, useState } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
ScrollView,
} from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { theme } from "./colors";
const STORAGE_KEY = "@toDos";
export default function App() {
const [working, setWorking] = useState(true);
const [text, setText] = useState("");
const [toDos, setToDos] = useState({});
useEffect(() => {
loadToDos();
}, []);
const travel = () => setWorking(false);
const work = () => setWorking(true);
const onChangeText = (payload) => setText(payload);
const saveToDos = async (toSave) => {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(toSave));
};
const loadToDos = async () => {
const s = await AsyncStorage.getItem(STORAGE_KEY);
s !== null ? setToDos(JSON.parse(s)) : null;
};
const addToDo = async () => {
if (text === "") {
return;
}
const newToDos = {
...toDos,
[Date.now()]: { text, working },
};
setToDos(newToDos);
await saveToDos(newToDos);
setText("");
};
return (
<View style={styles.container}>
<StatusBar style="auto" />
<View style={styles.header}>
<TouchableOpacity onPress={work}>
<Text
style={{ ...styles.btnText, color: working ? "white" : theme.grey }}
>
Work
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={travel}>
<Text
style={{
...styles.btnText,
color: !working ? "white" : theme.grey,
}}
>
Travel
</Text>
</TouchableOpacity>
</View>
<TextInput
onSubmitEditing={addToDo}
onChangeText={onChangeText}
returnKeyType="done"
value={text}
placeholder={working ? "Add a To Do" : "where do you want to go?"}
style={styles.input}
/>
<ScrollView>
{Object.keys(toDos).map((key) =>
toDos[key].working === working ? (
<View style={styles.toDo} key={key}>
<Text style={styles.toDoText}>{toDos[key].text}</Text>
</View>
) : null
)}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.bg,
paddingHorizontal: 20,
},
header: {
justifyContent: "space-between",
flexDirection: "row",
marginTop: 100,
},
btnText: {
fontSize: 38,
fontWeight: "600",
color: "white",
},
input: {
backgroundColor: "white",
paddingVertical: 15,
paddingHorizontal: 20,
borderRadius: 30,
marginVertical: 20,
fontSize: 18,
},
toDo: {
backgroundColor: theme.grey,
marginBottom: 10,
paddingVertical: 20,
paddingHorizontal: 20,
borderRadius: 10,
},
toDoText: {
color: "white",
fontSize: 16,
fontWeight: "500",
},
});
(colors.js)
toDoBg: "#1A1C20",
- 색상 코드 수정
'앱 개발 > React-Native' 카테고리의 다른 글
[React-Native] Todo 앱 만들기(6/6)_Delete(Alert) (0) | 2023.01.28 |
---|---|
[React-Native] Todo 앱 만들기(4/6)_Object.keys() (0) | 2023.01.28 |
[React-Native] Todo 앱 만들기(3/6)_Object.assign(), ES6 (0) | 2023.01.28 |
[React-Native] Todo 앱 만들기(2/6)_TextInput (0) | 2023.01.28 |
[React-Native] Todo 앱 만들기(1/6)_Touchables (0) | 2023.01.28 |