I am new to React and I am trying to build a very basic timesheet tool, where users can add tasks into the application and save. I am using React as well as Typescript.
So far I have my main component that includes an empty array of tasks and the table. Then inside the table body I have mapped to pull the table rows and a separate component that includes the tags.
I am trying to add in a button to delete tasks using the filter method on the array to remove the index for the individual item, however nothing happens when I click, or I get an undefined error. Can someone please help? I am using the delete button and handle delete function below to achieve this
Main Component
import * as React from 'react';
import { ITimesheetToolProps } from './ITimesheetToolProps';
import { escape } from '@microsoft/sp-lodash-subset';
import TableRow from './TableRow';
import styles from './TimesheetTool.module.scss';
export default class TimesheetTool extends React.Component<ITimesheetToolProps, any> {
state = {
tasks: []
}
addTask = (task) => {
const tasks = [...this.state.tasks];
this.setState({tasks: this.state.tasks.concat(task)});
}
handleDelete = (index) => {
const tasks = this.state.tasks.filter(t => t.index !== index);
this.setState({ tasks });
}
public render(): React.ReactElement<ITimesheetToolProps, any> {
return (
<div>
<button onClick={(task) => this.addTask(task)}>Add Task</button>
<table>
<thead className={styles.tbody}>
<tr>
<th>Project</th>
<th>Task</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saaturday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody className={styles.tbody}>
{this.state.tasks.map((task, index) => <tr key={index}><TableRow /><td><button onClick={() => this.handleDelete(index)}>Delete</button></td></tr>)}
</tbody>
</table>
</div>
);
}
}