diff --git a/package.json b/package.json new file mode 100644 index 0000000..9169755 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/react-todolist/src/App.jsx b/react-todolist/src/App.jsx index 45c4a00..8d213cc 100644 --- a/react-todolist/src/App.jsx +++ b/react-todolist/src/App.jsx @@ -1,8 +1,16 @@ -import {useState} from "react"; +import {useState, useEffect} from "react"; + +const STORAGE_STRING = 'todo-up-todos'; export default function App() { 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() { if (input.trim() === "") return; @@ -30,10 +38,14 @@ export default function App() {