Created
March 2, 2023 19:51
-
-
Save brianmugweru/efc3454e28ca026b5eb6161a75953e28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react'; | |
export default function TodoList () { | |
const [todo, setTodo] = useState('') | |
const [todoList, setTodoList] = useState([]) | |
const addTodo = () => { | |
if(!todo) return | |
setTodoList([...todoList, { text: todo, completed:false}]) | |
setTodo('') | |
} | |
const clickThrough = (i) => { | |
const newList = todoList.map((item, index) => { | |
if(i === index) return { | |
text: item.text, | |
completed: !item.completed | |
} | |
return item | |
}) | |
setTodoList(newList) | |
} | |
return ( | |
<> | |
<div> | |
<h2> | |
Todo List | |
</h2> | |
<input value={todo} onChange={(e) => setTodo(e.target.value)} /> | |
<button onClick={() => addTodo()}> Add </button> | |
</div> | |
<p className='task-counter'> {todoList.filter((i) => !i.completed).length} remaining out of {todoList.length} tasks </p> | |
<ul> | |
{todoList.map((task, index) => ( | |
<li onClick={() => clickThrough(index)} className={task.completed ? 'is-done' : ''} key={index}> | |
{task.text} | |
</li> | |
))} | |
</ul> | |
<style>{` | |
.is-done { | |
text-decoration: line-through; | |
} | |
`}</style> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment