When I click my Add Category button, nothing happens. I want to be able to add tasks and add new categories. When I add a category, the category should be added to the category array list and attached to the tasks array.
I have tried to create an addCats(e) function and added to the Add Category button's on click.
class TaskBar extends React.Component
{
constructor(props){
super(props);
this.state = {
tasks:[],
task: '',
categories:["Home","Work","Play","X"],
cats: ''
};
this.addTask = this.addTask.bind(this);
this.addCats = this.addCats.bind(this);
}
addCats(e){
if(this.state.cats !=="")
{
var newCategory = {
text: this.state.cats,
key: Date.now(),
};
this.setState(() => {
this.state.categories.push(newCategory);
});this.setState({cats: ''});
}
}
addTask(e){
// console.log(this.state.task);
if(this.state.task !== "")
{
var newTask = {
text: this.state.task,
key: Date.now(),
categories:[]
};
this.setState(() => {
this.state.tasks.push(newTask);
});
this.setState({task: ''});
// console.log(this.state.task);
}
//console.log(this.state.tasks);
}
componentDidUpdate(){
console.log(this.state.tasks);
console.log(this.state.categories)
}
render(){
return (
<div className="inputArea cols-md-6">
<input
value = {this.state.task}
placeholder = "Enter task"
onChange = {(event) => this.setState({task:event.target.value})}
/>
<input type="button" value="Add Task" onClick={this.addTask} />
<br /><br />
<input
value = {this.state.cats}
placeholder = "Add Category"
onChange = {(event) => this.setState({cats:event.target.value})}
/>
<input type="button" value="Add Category" onClick={this.addCats} />
<br /><br />
<div>
<TaskList tasks={this.state.tasks} categories={this.state.categories}/>
</div>
</div>
)
}
}
export default TaskBar;
When I click Add Category,the page refreshes but displays nothing.