I want to do a to do list with hooks. Here is my related code.
function generateId() {
return '_' + Math.random().toString(36).substr(2, 9);
};
export function TodoList(){
const [todos, setTodos] = React.useState([])
const [input, setInput] = React.useState('')
const [time, setTime] = React.useState('')
const handleSubmit = () => {
setTodos((todos) => todos.concat({
text: input,
id: generateId(),
timeRequired: time,
}))
setInput('')
setTime('')
}
// remove to do works fine.
const removeTodo = (id) => setTodos((todos) => todos.filter((todo) => todo.id !== id ))
/// confusion here
let todo = (id) => todos.find(x => x.id = id)
const decrement = (id) => setTodos((todo(id).timeRequired) => timeRequired - 1)
///
return(
<React.Fragment>
<input type="text" value={input} placeholder="New Task" onChange={(e) => setInput(e.target.value)}>
</input>
<input type="number" value={time} placeholder="Hours Required" onChange={(e) => setTime(e.target.value)}>
</input>
<button onClick={handleSubmit}> Submit </button>
<ul>
{todos.map(({text, id, timeRequired}) => (
<li key={id}>
<span>{text}: remaining {timeRequired} hours </span>
<button onClick={() => removeTodo(id)}>
Cancel ❌
</button>
<button onClick={() => decrement(id)}> Decrement ➖ </button>
<button > Increase ➕ </button>
<button> Done ✔️ </button>
</li>
))}
</ul>
</React.Fragment>
)
}
So I want to increment/decrement the time remaining on the todo list. However, I don't know how to choose the specific item in the list and change one property ( time remaining) and keep the text property.
Thanks.