Daten der Todo Apps persistiert
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "react-basics-2",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://gt.mbrnd.de/Max/react-basics.git"
|
||||||
|
},
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"vite": "^8.1.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,16 @@
|
|||||||
import {useState} from "react";
|
import {useState, useEffect} from "react";
|
||||||
|
|
||||||
|
const STORAGE_STRING = 'todo-up-todos';
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
const [input, setInput] = useState("");
|
const [input, setInput] = useState("");
|
||||||
const [todos, setTodos] = useState([]);
|
const [todos, setTodos] = useState(JSON.parse(localStorage.getItem(STORAGE_STRING))||[]);
|
||||||
|
|
||||||
|
useEffect(() => persistTodos(),[todos]);
|
||||||
|
|
||||||
|
function persistTodos() {
|
||||||
|
localStorage.setItem(STORAGE_STRING, JSON.stringify(todos));
|
||||||
|
}
|
||||||
|
|
||||||
function addTodo() {
|
function addTodo() {
|
||||||
if (input.trim() === "") return;
|
if (input.trim() === "") return;
|
||||||
@@ -30,10 +38,14 @@ export default function App() {
|
|||||||
</div>
|
</div>
|
||||||
<ul id="todo-list-container">
|
<ul id="todo-list-container">
|
||||||
{todos.map((todo) => (
|
{todos.map((todo) => (
|
||||||
<li>
|
<li key={todo.id}>
|
||||||
<span onClick={() => toggleTodo(todo.id)}>
|
<label>
|
||||||
{todo.text}: {todo.erledigt ? "bereits " : "noch nicht "} erledigt
|
<input
|
||||||
</span>
|
type="checkbox"
|
||||||
|
checked={todo.erledigt}
|
||||||
|
onChange={() => toggleTodo(todo.id)}
|
||||||
|
/>{todo.text}
|
||||||
|
</label>
|
||||||
<button onClick={() => deleteTodo(todo.id)}>Entfernen</button>
|
<button onClick={() => deleteTodo(todo.id)}>Entfernen</button>
|
||||||
</li>))}
|
</li>))}
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react'
|
|||||||
// https://vite.dev/config/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react()],
|
plugins: [react()],
|
||||||
|
server: {
|
||||||
|
port: 3006
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,8 +3,16 @@ import TodoForm from "./components/TodoForm";
|
|||||||
import TodoList from "./components/TodoList";
|
import TodoList from "./components/TodoList";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
|
|
||||||
|
const STORAGE_STRING = 'todo-app2-todos';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [todos, setTodos] = useState([]);
|
const [todos, setTodos] = useState(JSON.parse(localStorage.getItem(STORAGE_STRING)) || []);
|
||||||
|
|
||||||
|
useEffect(() => persistTodos(), [todos]);
|
||||||
|
|
||||||
|
function persistTodos() {
|
||||||
|
localStorage.setItem(STORAGE_STRING, JSON.stringify(todos));
|
||||||
|
}
|
||||||
|
|
||||||
function addTodo(text, priority) {
|
function addTodo(text, priority) {
|
||||||
const newTodo = {
|
const newTodo = {
|
||||||
|
|||||||
Reference in New Issue
Block a user