I have made To-Do list with react, but when I submit new entry, it wont show on the list. I have no clue where the problem is.
class TodoList extends React.Component {
constructor(){
super();
This array doesn't get updated like it should:
this.state ={
items:["item1", "item2"]
};
}
Here I add new task:
addTask = (task) => {
this.setState((prevState) => ({
items: prevState.items.concat([task])
}));
}
render(){
return(
<div>
<AddTodo addNew ={this.addTask}/>
<ShowEntries entries={this.state.items}/>
</div>
)
}
}
class AddTodo extends React.Component {
constructor(){
super();
this.handeAddNew = this.handeAddNew.bind(this);
this.state ={
task: ""
};
}
Here I update the state, it works fine:
update(e){
this.setState({
task: e.target.value
});
}
Here I have passed the addTask function as a prop. This might be where the problem is?
handeAddNew(){
this.props.addNew(this.state.task);
this.setState({
task: ""
});
}
render(){
return(
<div>
<input type="text"
onChange={this.update.bind(this)}></input>
<button onClick={this.handleAddNew}>Add</button>
</div>
)
}
}
ShowEntries class works fine too:
class ShowEntries extends React.Component{
render(){
var list =this.props.entries.map((task) =>{
return <li>{task}</li>
});
return(
<div>
<ul>
{list}
</ul>
</div>
)
}
}
export default TodoList
Any ideas?