Skip to content

Instantly share code, notes, and snippets.

@EmmanuelObua
Last active August 23, 2023 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmmanuelObua/abb414612b8bb3dd979d1bc4b670d01b to your computer and use it in GitHub Desktop.
Save EmmanuelObua/abb414612b8bb3dd979d1bc4b670d01b to your computer and use it in GitHub Desktop.
import React from 'react';
import { TaskConsumer } from './TaskContext';
function TaskList() {
// Here we are actually distructing the tasks and dispatch methods from the TaskConsumer which has the
// taskContext object.
return (
<TaskConsumer>
{({ tasks, dispatch }) => (
<>
{tasks.map(task => (
<div key={task.id} className="d-flex justify-content-between p-1">
<input type="checkbox" checked={task.completed}
onChange={() => dispatch({type: 'UPDATE_TASK', payload: { id: task.id, completed: !task.completed } }) }
/>
{task.title}
{!task.completed && (
<button className="btn btn-sm btn-danger" style={{ borderRadius:100 }} onClick={() => dispatch({ type: 'DELETE_TASK', payload: task.id }) }>X</button>
)}
</div>
))}
</>
)}
</TaskConsumer>
);
}
export default TaskList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment