【问题标题】:ReactJS hover/mouseover effect for one list item instead of all list items一个列表项而不是所有列表项的 ReactJS 悬停/鼠标悬停效果
【发布时间】:2018-11-11 00:14:13
【问题描述】:

我是 React 新手,所以这可能看起来很简单,或者可能不是,我不确定。我正在建立一个基本的待办事项清单。我希望列表项上的鼠标悬停效果弹出“删除此”文本。但是到目前为止,对于我的代码,当我将鼠标悬停在一个列表项上时,会为所有列表项弹出“删除这个”,而不仅仅是一个。

当我尝试通过为单个列表项创建一个新组件来解决这个问题时,这似乎不起作用。非常感谢任何帮助!

class ToDosContainer extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      heading: 'Something You Need To Do?',
      todos: [
        'wake up',
        'brush your teeth'
      ],
    }

    this.addToDo = this.addToDo.bind(this)
  }

  addToDo(todo) {
    this.setState((state) => ({
      todos: state.todos.concat([todo])
    }))
  }
  render() {
    return (
      <div>
        <h1>To Do List</h1>
        <h3>{this.state.heading}</h3>
        <AddToDo addNew={this.addToDo} />
        <ShowList tasks={this.state.todos} />
      </div>  
    )
  }
}

class AddToDo extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      newToDo: ''
    }

    this.updateNewToDo = this.updateNewToDo.bind(this)
    this.handleAddToDo = this.handleAddToDo.bind(this)

  }

  //I think I can delete this part
  updateNewToDo(e) {
    this.setState({
      newToDo: e.target.value
    })
  }
 //

  handleAddToDo() {
    this.props.addNew(this.state.newToDo)
    this.setState({
      newToDo: ''
    })
  }

  render() {
    return (
      <div>
        <input
          type="text"
          value={this.state.newToDo}
          onChange={this.updateNewToDo}
         />
        <button onClick={this.handleAddToDo}> Add To Do </button>
      </div>
    )
  }
}

class ShowList extends React.Component {

  constructor(props) {
    super(props)

    this.state = {
      newToDo: ''
    }
  }

  onMouseOver(e) { 
    this.setState({
      text: 'delete me'
    })
    console.log('hey')
  }

  onMouseOut(e) { 
     this.setState({
      text: ''
    })
     console.log('hey hey')

  }

  render() {
   const { text } = this.state;
   return (
    <div>
       <h4>To Do's</h4>
       <ul>
          {this.props.tasks.map((todo) => {
            return <li onMouseEnter={this.onMouseOver.bind(this)} onMouseLeave={this.onMouseOut.bind(this)}> {todo} {text}</li>
          })}
       </ul>
    </div>
   ) 
  }
}

ReactDOM.render(<ToDosContainer />, document.getElementById('helloworld')); 

【问题讨论】:

    标签: javascript reactjs hover components


    【解决方案1】:

    我会制作一个任务组件。您不想在ListView 组件中设置文本的状态,因为this.state.text 由地图中的每个任务共享。每个任务都应该知道自己的悬停,而不是其他子项的悬停。

    class Task extends React.Component {
      state = {
        text: ""
      };
      onMouseOver(e) {
        this.setState({
          text: "delete me"
        });
      }
    
      onMouseOut(e) {
        this.setState({
          text: ""
        });
      }
    
      render() {
        return (
          <li
            onMouseEnter={this.onMouseOver.bind(this)}
            onMouseLeave={this.onMouseOut.bind(this)}
          >
            {this.props.todo} {this.state.text}
          </li>
        );
      }
    }
    
    
    class ShowList extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          newToDo: ""
        };
      }
    
      render() {
        const { text } = this.state;
        return (
          <div>
            <h4>To Do's</h4>
            <ul>
              {this.props.tasks.map(todo => {
                return <Task todo={todo} />;
              })}
            </ul>
          </div>
        );
      }
    }
    

    【讨论】:

    • 非常感谢!在我尝试了解 React 时非常有帮助。
    • 再次感谢您上周的帮助。在过去的一周里,我一直在试图弄清楚如何在 React 中真正通过单击删除列表项。这是我到目前为止所做的,没有任何效果。我怀疑第 95 行有什么东西:codepen.io/bfoley650/pen/mKPgdE?editors=0011
    • @bfoley_teamug 我喜欢你的忙碌,干得好!所以,再一次,你真的很接近。与将 addTask 处理程序从 TodosContainer 传递给 AddTodo 组件的方式相同,您必须将 removeTask 从 TodosContainer 传递给 ShowList 组件。但是,一旦到达那里,您必须从 ShowList 组件进一步传递一个级别到 Task 本身。一旦在构造函数中绑定,您的操作可以传递给任意数量的子代,并且能够修改父代的状态。这是带有更改的代码笔:codepen.io/anon/pen/RJRRdP?editors=0011
    • 非常感谢,非常感谢
    • 您能否告诉我第 145 行的这段代码发生了什么:key={todo-${key}-${todo}}。再次非常感谢您帮助我!
    猜你喜欢
    • 2013-06-24
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2012-09-20
    相关资源
    最近更新 更多