简体   繁体   English

根据另一个元素的事件来操作多个元素的React方法是什么?

[英]What's the React way to manipulate multiple elements according to event of another element?

I am confused about this for a long time. 我对此很困惑。
Here is the case: 就是这种情况:
1, I create a table with multiple rows, in this way: 1,我用这种方式创建一个包含多行的表:

    tableRow(basicInfoArray) {
    return basicInfoArray.map((basicInfo, index) => (
      <tr
        key={basicInfo._id}
        className={index % 2 === 0 ? 'alt' : null}
        onClick={event => this.props.showDetail(basicInfo._id, event)}
      >
        <td>{basicInfo.mentee_id}</td>
        <td>{`${basicInfo.firstname} ${basicInfo.lastname}`}</td>
        <td>{basicInfo.othername}</td>
        <td>{basicInfo.location}</td>
      </tr>
    ));
  }
  1. As you can see, I bind a onClick event to each row. 如您所见,我将onClick事件绑定到每一行。 If the row is clicked, it will highlight, and there will be a drilldown to show detail information. 如果单击该行,它将突出显示,并且将进行向下钻取以显示详细信息。
  2. The question is here, after clicked on the backdrop(which bind a onClick event), the drilldown hide and I should remove the highlight effect from the row. 问题是在这里,单击背景(绑定了onClick事件)后,向下钻取隐藏,我应该从行中删除突出显示效果。 Currently I use this way: 目前,我使用这种方式:

     const highLightRows = document.getElementsByClassName('highLight'); for (let i = 0; i < highLightRows.length; i += 1) { highLightRows[i].classList.toggle('highLight'); } 

As the documents of React.js says that it's not a good practice to manipulate the dom directly, the UI change should be caused by the props/state change. 正如React.js的文档所述,直接操作dom不是一个好习惯,UI的改变应该由props / state的改变引起。 Obviously it's not a good idea to bind a state for each row of the table because of the quantity. 显然,由于数量原因,为表的每一行绑定一个状态不是一个好主意。 What's the best practice to do this? 最佳做法是什么?

It's important to keep in mind that react renders whatever you have in memory, in this case you have an array of items that you want to display in a table, when clicking any of those items you want to highlight the selected, right? 重要的是要记住,react会渲染您内存中的任何内容,在这种情况下,当您单击要突出显示所选内容的任何项目时,您要在表中显示一组项目。

Well, for that you could define a property in each element of the array called selected , this property will be true/false depending on the user selection, then when rendering the row you will check for this property and if it's there you will assign the highLight class or not. 好吧,为此,您可以在名为selected的数组的每个元素中定义一个属性,该属性将为true / false,具体取决于用户选择,然后在渲染该行时将检查该属性,如果存在,则将分配该属性。 highLight类与否。 With this approach you will only need to worry to change the value of this property on memory and it will automatically get highlighted on the DOM. 使用这种方法,您只需要担心更改此属性在内存中的值,它将自动在DOM上突出显示。

Here's an example: 这是一个例子:

renderRow(info, index) {
  const styles = [
    index % 2 === 0 ? 'alt' : '',
    info.selected = 'highLight' : '',
  ];

  return (
    <tr
      key={info._id}
      className={styles.join(' ')}
      onClick={event => this.props.showDetail(info, event)}
    >
      <td>{basicInfo.mentee_id}</td>
      <td>{`${info.firstname} ${info.lastname}`}</td>
      <td>{info.othername}</td>
      <td>{info.location}</td>
    </tr>
  );
}

renderContent(basicInfoArray) {
  return basicInfoArray.map((basicInfo, index) => this.rendeRow(basicInfo, index));
}

Just make sure to set to true the selected property on showDetail function, and then set to false when you need to hide and remove the highLight class. 只要确保将showDetail函数的selected属性设置为true ,然后在需要隐藏和删除highLight类时将其设置为false highLight

Good luck! 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用React,呈现具有多个嵌套元素的元素的正确方法是什么? - Using React, what is the proper way to render an element with multiple nested elements? 使用相同的事件处理程序分别使用不同的事件绑定多个元素的优雅方法是什么? - What's the elegant way of binding multiple elements with different event respectively using same event handler? 流星和React:点击时操纵元素的正确方法? - Meteor and React: Correct way to manipulate elements on click? 创建具有多个属性的元素并将其附加到另一个元素并将其用作变量的最有效方法是什么? - What's the effectiviest way of creating an element with multiple attributes and appending it to another element and have it available as a variable?? 在React + Redux.js应用程序中,在Provider组件之外操作DOM元素的正确方法是什么? - What is a proper way to manipulate DOM elements outside of Provider component in React + Redux.js app? 在子代组件中操纵道具的redux-react方法是什么 - What's the redux-react way to manipulate props in react children component 基于另一个元素的高度更改元素样式的angularjs方法是什么 - What is the angularjs way of changing an elements style based on the height of another element 删除元素上的事件侦听器的最佳方法是什么? - What's the best way to remove event listener on an element? om / react:在渲染目标元素之外操作元素 - om/react: manipulate elements outside the render target element 在 React 中更改另一个组件的 state 的正确方法是什么 - What's the right way to change the state of another component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM