لقد رفعت الخطافات الى الاعلى وظهر اخطاء اكثر
import React,{useState,useRef, useEffect} from "react";
import './App.css'
import logo from './assets/react.svg'
function App(){
const [todos , setTodos] = useState([]);
const inpRef = useRef();
/// قراءة البيانات من التخزين المحلي عند تحميل الصفحة أول مرة
useEffect(() => {
const storedTodos = localStorage.getItem("todos");
if (storedTodos) {
setTodos(JSON.parse(storedTodos));
}
}, []);
// حفظ المهام في التخزين المحلي عند كل تغيير في القائمة
useEffect(() => {
localStorage.setItem("todos", JSON.stringify(todos));
}, [todos]);
const handelButtonClick = () => {
const text = inpRef.current.value;
const newTodos = {compiled: false, text}
inpRef.current.value = '';
setTodos([...todos,newTodos])
}
const handelItemClick = (i) => {
const newItem = [...todos];
newItem[i].compiled = !newItem[i].compiled;
setTodos(newItem)
}
const handelDeleteClick = (i) => {
const newDelete = [...todos];
newDelete.splice(i,1)
setTodos(newDelete);
}
return(
<>
<img src={logo} alt="" />
<div className="App">
<h2>To Do List</h2>
<ul>
{todos.map(({text,compiled}, i) => {
return <div className="item"><li key={i} className={compiled ? 'done' : ''} onClick={() => handelItemClick(i)}>{text}</li><span onClick={() => handelDeleteClick(i)}>❌</span></div>
})}
</ul>
<input ref={inpRef} placeholder="Enter item..."/>
<button onClick={handelButtonClick}>Add</button>
</div>
</>
)
}
export default App;