I'm trying to make the function to delete my todo item but when click on the delete button, nothing happens, is there anything wrong with my codes, plsease help me! Thank you so much!
import React, { useState } from "react";
function App() {
const [value, setValue] = useState("");
const [todos, setTodos] = useState([]);
// you can use the submit itself, no need for an extra addTodo function
handleSubmit = (e) => {
console.log("x");
e.preventDefault();
setTodos([...todos, value]);
setValue("");
};
deleteTodo = (id) => {
console.log("delete")
const removedArr = [...todos].filter(todo => todo.id !== id);
setTodos(removedArr);
}
return (
<div>
<form onSubmit={handleSubmit}>
<input
onChange={(e) => setValue(e.target.value)}
type="text"
placeholder="add todo"
/>
<button type="submit">Add</button>
</form>
{todos.map((todoValue) => (
<div>
{todoValue} <button deleteTodo={()=>deleteTodo(id)}>x</button>
</div>
))}
</div>
);
}
export default App;
Link codesandbox: https://codesandbox.io/s/tender-cdn-cy3w4?file=/src/App.js:0-933