Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create To-Do List application #251

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Samyukta248
Copy link

import React, { useState, useEffect } from 'react';
import './App.css'; // Import CSS file

function App() {
const [tasks, setTasks] = useState(() => {
const storedTasks = localStorage.getItem('tasks');
return storedTasks ? JSON.parse(storedTasks) : [];
});
const [newTask, setNewTask] = useState('');

useEffect(() => {
localStorage.setItem('tasks', JSON.stringify(tasks));
}, [tasks]);

const addTask = () => {
if (newTask.trim() !== '') {
setTasks([...tasks, { text: newTask, completed: false }]);
setNewTask('');
}
};

const removeTask = (index) => {
const newTasks = [...tasks];
newTasks.splice(index, 1);
setTasks(newTasks);
};

const toggleComplete = (index) => {
const newTasks = [...tasks];
newTasks[index].completed = !newTasks[index].completed;
setTasks(newTasks);
};

return (


To-Do List



<input
type="text"
value={newTask}
onChange={(e) => setNewTask(e.target.value)}
placeholder="Add a new task"
onKeyDown={(e) => {
if (e.key === 'Enter') {
addTask();
}
}}
/>
Add


    {tasks.map((task, index) => (
    <li key={index} className={task.completed ? 'completed' : ''}>
    <span onClick={() => toggleComplete(index)} style={{textDecoration: task.completed ? 'line-through': 'none'}}>{task.text}
    <button onClick={() => removeTask(index)}>Remove

    ))}


);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants