import React, { useEffect, useState } from 'react' export default function Todos() { let [todos, setTodos] = useState([]); useEffect(() => { // get data for key todos from localStorage let todoItems = window.localStorage.getItem("todos") if (todoItems) { setTodos(JSON.parse(todoItems)) } }, []) function getNextId() { // find out id for last item and add 1 to it if (todos.length === 0) return 1; return todos[todos.length - 1].id + 1 } function addTodo(todoText) { let nextId = getNextId() let newTodos = [...todos, { id: nextId, text: todoText }] updateLocalStorage(newTodos) setTodos(newTodos) } function deleteTodo(id) { let newTodos = todos.filter((todo) => todo.id !== id) setTodos(newTodos) updateLocalStorage(newTodos) } function updateLocalStorage(todos) { window.localStorage.setItem("todos", JSON.stringify(todos)) } return ( <> <h1>Todos</h1> <AddTodo addTodo={addTodo} /> <p></p> <ListTodos todos={todos} todoDone={deleteTodo} /> </> ) } function AddTodo({addTodo}) { let [todoText, setTodoText] = useState("") function addNewTodo(e) { e.preventDefault(); addTodo(todoText) setTodoText("") } return ( <form onSubmit={addNewTodo}> Todo : <input type="text" value={todoText} required onChange={(e) => setTodoText(e.target.value)} /> <input type="submit" value="Add" /> </form> ) } function ListTodos({ todos, todoDone }) { return ( <ul> {todos.map((todo) => <li key={todo.id}>{todo.text} <button className="btn btn-link" onClick={() => todoDone(todo.id)}>Done</button></li>)} </ul> ) }
// get data for key todos from localStorage let todoItems = window.localStorage.getItem("todos") if (todoItems) { setTodos(JSON.parse(todoItems)) }
function updateLocalStorage(todos) { window.localStorage.setItem("todos", JSON.stringify(todos)) }
<ListTodos todos={todos} todoDone={deleteTodo} />
<AddTodo addTodo={addTodo} />
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import Todos from './Todos'; ReactDOM.render(, document.getElementById('root'));
import React from 'react'; import ReactDOM from 'react-dom/client'; import Todos from './Todos'; import './index.css'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render();
<!DOCTYPE html> <html lang="en"> <head> <title>React App</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div class="container" id="root"></div> </body> </html>