Skip to content

Instantly share code, notes, and snippets.

@Soumyarian98
Created November 11, 2024 06:15
Show Gist options
  • Select an option

  • Save Soumyarian98/5f2fbbd77b39c9453eb98f06548a95e2 to your computer and use it in GitHub Desktop.

Select an option

Save Soumyarian98/5f2fbbd77b39c9453eb98f06548a95e2 to your computer and use it in GitHub Desktop.
import React, { useCallback } from "react";
export const ToDoApp = () => {
const handleTaskClick = useCallback((taskId: string) => {
return (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
alert(`Task with ID: ${taskId} clicked`);
};
}, []);
const tasks = [
{ id: "task1", name: "Buy Groceries" },
{ id: "task2", name: "Walk the Dog" },
{ id: "task3", name: "Pay Bills" },
];
return (
<div>
{tasks.map((task) => (
// <button key={task.id} onClick={(e) => handleTaskClick(task.id)}> ❌
<button key={task.id} onClick={handleTaskClick(task.id)}>
{task.name}
</button>
))}
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment