I am making a todo app in react and after taking input from the user on submit i am making a post request to update in the database and then updating the state. and then i am trying to clear the input field using e.target.value = "". But this is not working. Iam fairly new to JS and React. can some one point me what i am doing wrong here.
class TodoApp extends Component {
constructor(props) {
super(props);
this.state = {
todos: [],
};`enter code here
this.handleTodos = this.handleTodos.bind(this);
this.handleLogout = this.handleLogout.bind(this);
this.removeTodo = this.removeTodo.bind(this);
};
componentDidMount() {
const authStr = 'Bearer ' + getJWTToken();
axios.get('/tasks', {
'headers': {
'Authorization': authStr
}
}).then(res => {
// console.log(res.data);
this.setState({
todos: res.data,
})
}).catch(err => {
console.log(err);
});
};
removeTodo = id => {
// console.log(id)
const authStr = 'Bearer ' + getJWTToken();
axios.delete('/tasks/' + id, {
'headers': {
'Authorization': authStr
}
}).then(res => {
// console.log(res.data);
let newTodos = [...this.state.todos];
newTodos = newTodos.filter(todo => {
return todo._id !== id;
});
//Update the State
this.setState({
todos: newTodos
});
}).catch(err => {
console.log(err);
});
};
handleTodos = e => {
e.preventDefault();
const authStr = 'Bearer ' + getJWTToken();
var todo = {
description: e.target.value
}
console.log(todo)
axios.post('/tasks', todo, {
'headers': {
'Authorization': authStr
}
}).then(res => {
// console.log(res.data);
this.setState({
todos: this.state.todos.concat(res.data)
})
}).catch(err => {
console.log(err)
});
e.target.value = "";
// console.log(todo);
};
handleLogout() {
localStorage.removeItem('jwtToken');
this.props.history.push("/");
}
render() {
const listLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 8 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 },
},
};
return (
<div className="container-fluid App">
<div className="todoContainer">
<Header
handleLogout={this.handleLogout}
/>
<h1 style={{ paddingTop: "10px" }}>TODO App</h1>
<Input
placeholder="What needs to be done?"
onPressEnter={this.handleTodos}
/>
<List
itemLayout="horizontal"
locale={{ emptyText: "No Todos" }}
dataSource={this.state.todos}
renderItem={item => (
<TodoItem
todo={item}
removeTodo={this.removeTodo}
/>
)}
/>
</div>
</div>
);
};
};
export default TodoApp;