The onClick event is not firing at all.I am able to add todos and toggle todos but when i click on Completed todos or NotCompleted todos nothing happens.Here is the complete code for the class. There is an add to do which adds the todos.then there is a toggle to do which strikes or unstrikes a todo.the problem that I am having is with the CompletedTodos and NotCompletedTodos list items.
class TodoList extends Component {
render() {
console.log(this.props.todos)
return (
<div className='todo'>
<form id="frm1">
Add To Do: <input type="text" name="fname" id="todo" /><br></br>
<input type="button" onClick={()=> this.props.actions.addTodo(document.getElementById("todo").value)} value="Submit" />
</form>
<div className="visible">
<ul className=" visibility">
<li onClick={ () => {
return(<div> {this.props.todos.map(todo => {
return (<p> {todo.text} </p>)
})}</div>)
}}>
AllToDos
</li>
<li onClick={() => {
return (<div> {this.props.todos.filter(todo => {
if(todo.completed===true) {
return (<p> {todo.text} </p>)
}
})}</div>)
}}>
CompletedToDos
</li>
<li onClick={() => {
return ( <div> {this.props.todos.filter(todo => {
if(todo.completed===false) {
return (<p> {todo.text} </p>)
}
})}</div>)
}}>
NotCompletedToDos
</li>
</ul>
</div>
<ul className='todo__list'>
{this.props.todos.map(t => (
<li key={t.id} className='todo__item' onClick={() => this.props.actions.toggleTodo(t.id)}>
<Todo todo={t} />
</li>
))}
</ul>
</div>
)
Nothing happens because, onclick on List
elements returns a DOM but nowhere you are displaying it.
You should do it like
class TodoList extends Component {
state = {
displayTodos: []
}
render() {
console.log(this.props.todos)
return (
<div className='todo'>
<form id="frm1">
Add To Do: <input type="text" name="fname" id="todo" /><br></br>
<input type="button" onClick={()=> this.props.actions.addTodo(document.getElementById("todo").value)} value="Submit" />
</form>
<div className="visible">
<ul className=" visibility">
<li onClick={ () => {
var todos = []
todos = this.props.todos.map(todo => {
return (<p> {todo.text} </p>)
})
this.setState({displayTodos: todos})
}}>
AllToDos
</li>
<li onClick={() => {
var todos = [];
this.props.todos.filter(todo => {
if(todo.completed===true) {
todos.push(<p> {todo.text} </p>)
}
})
this.setState({displayTodos: todos})
}}>
CompletedToDos
</li>
<li onClick={() => {
var todos = []
this.props.todos.filter(todo => {
if(todo.completed===false) {
todos.push(<p> {todo.text} </p>)
}
})
this.setState({displayTodos: todos})
}}>
NotCompletedToDos
</li>
</ul>
<div>{this.state.displayTodos}</div>
</div>
<ul className='todo__list'>
{this.props.todos.map(t => (
<li key={t.id} className='todo__item' onClick={() => this.props.actions.toggleTodo(t.id)}>
<Todo todo={t} />
</li>
))}
</ul>
</div>
)
}
}
Also I would personally call a function onClick
rather than do everyting inline to increase code readability
sorry, But I don't know what's the target of click function,
it don't change the Component state or props, so this component will not render.
<li onClick={() => {
return (<div> {this.props.todos.filter(todo => {
if(todo.completed===true) {
return (<p> {todo.text} </p>)
}
})}</div>)
}}>
CompletedToDos
</li>
<li onClick={() => {
return ( <div> {this.props.todos.filter(todo => {
if(todo.completed===false) {
return (<p> {todo.text} </p>)
}
})}</div>)
}}>
NotCompletedToDos
</li>