So I'm doing the splice method usually when I use it but it's not working in this situation... I have no idea why. When I pass the deleteToDo function it deletes everything. Like 4 items... I was trying to explain the concept to someone but I guess I don't understand because I wrote this to explain it and it didn't work lol. Oops!
I thought this would remove the specific item by index, but it doesn't work. Can anyone explain what I'm doing wrong? It looks similar to other solutions I've seen here but idk. Thanks in advance!
Specifically these lines:
// Deletes item from todos
const deleteToDo = (toDo) => {
let results = todos.splice(toDo, 1);
setTodos(results);
};
Here's the simple code in it's entirety.
import React, { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
const App = () => {
// State Managment
const [todos, setTodos] = useState([]);
const [name, setName] = useState("Logan");
const [todo, setTodo] = useState("");
// set's todo as input box
const updateToDO = (e) => {
setTodo(e.target.value);
};
// Updates todos state
const updateList = (e) => {
const results = todos.concat(todo);
setTodos(results);
setTodo("");
};
// Deletes item from todos
const deleteToDo = (toDo) => {
let results = todos.splice(toDo, 1);
setTodos(results);
};
return (
<div className="container">
<h1>Todo's for {name} </h1>
<input value={todo} onChange={updateToDO} type="text" />
<button onClick={updateList}>Add To Do</button>
<AnimatePresence>
{/* loops through todos */}
{todos.map((todo, id) => (
<motion.div
key={id}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="card"
>
<div className="card-body">
<h3>{todo}</h3>
<button
onClick={() => {
deleteToDo(id);
}}
>
Delete
</button>
</div>
</motion.div>
))}
</AnimatePresence>
</div>
);
};
export default App;