import { useRef, useState } from "react"; import { FlatList, KeyboardAvoidingView, Platform, Pressable, StyleSheet, Text, TextInput, View } from "react-native"; import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context"; type Lesson = { id: string; title: string; done: boolean }; export default function App() { const [lessons, setLessons] = useState([ { id: "html", title: "HTML 表单", done: false }, { id: "css", title: "CSS 布局", done: true }, ]); const [draft, setDraft] = useState(""); const [onlyPending, setOnlyPending] = useState(false); const [error, setError] = useState(""); const nextId = useRef(1); const visible = lessons.filter(item => !onlyPending || !item.done); function add() { const title = draft.trim(); if (!title) { setError("请输入学习内容"); return; } const id = "new-" + nextId.current++; setLessons(current => [...current, { id, title, done: false }]); setDraft(""); setError(""); } function toggle(id: string) { setLessons(current => current.map(item => item.id === id ? { ...item, done: !item.done } : item)); } return 学习记录板 学习内容 添加 {error ? {error} : null} setOnlyPending(value => !value)}> {onlyPending ? "已启用:" : "未启用:"}只看待学 显示 {visible.length} 项 item.id} keyboardShouldPersistTaps="handled" ListEmptyComponent={没有匹配的记录} renderItem={({ item }) => toggle(item.id)}> {item.done ? "已学:" : "待学:"}{item.title} } /> 数据只保存在本次运行中 ; } const styles = StyleSheet.create({ safe: { flex: 1, backgroundColor: "#fafafa" }, page: { flex: 1, padding: 20, gap: 12, width: "100%", maxWidth: 720, alignSelf: "center" }, title: { fontSize: 24, fontWeight: "600" }, input: { borderWidth: 1, borderColor: "#777", borderRadius: 4, padding: 12, fontSize: 16 }, button: { minHeight: 48, padding: 12, backgroundColor: "#e4e8f3", justifyContent: "center" }, item: { minHeight: 48, paddingVertical: 12, borderBottomWidth: 1, borderBottomColor: "#ddd" }, error: { color: "#a11a26" }, });