本文介绍了在React Redux ToDoList应用程序中未触发onClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onClick事件根本没有触发。我能够添加待办事项和切换待办事项,但是当我单击Completed todos或NotCompleted todos时,什么也没有发生。这是该类的完整代码。有一个要做的事情添加了待办事项。然后有一个切换来完成或取消待办事项。我遇到的问题是CompletedTodos和NotCompletedTodos列表项。

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>
)


推荐答案

什么都没有发生,因为onclick List 元素返回一个DOM,但无处显示。

Nothing happens because, onclick on List elements returns a DOM but nowhere you are displaying it.

您应该这样做

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>
        )
      }
    }

我也个人称为函数 onClick 而不是进行内联以增加代码可读性

Also I would personally call a function onClick rather than do everyting inline to increase code readability

这篇关于在React Redux ToDoList应用程序中未触发onClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:06