When using ReactJS, hovering over a row in a table should trigger a change in the background color of the corresponding row in another table with the same index

I need to create a feature where hovering over a row in the first table will highlight a corresponding row in the second table. The highlighting should be based on the index of the hovered row.

Here is an example:

<div>
    <table>
        <tr><td>table 1 row 1</td></tr> // => hovered
        <tr><td>table 1 row 2</td></tr>
        <tr><td>table 1 row 3</td></tr>
    </table>
</div>
<div>
    <table>
        <tr><td>table 2 row 1</td></tr> // => highlighted
        <tr><td>table 2 row 2</td></tr>
        <tr><td>table 2 row 3</td></tr>
    </table>
</div>

Answer №1

For the initial table, incorporate an onMouseEnter/onMouseLeave event that allows you to establish a state containing the index of the row being hovered over

<tr onMouseEnter={() => setRowToHover(1)}><td>table 1 row 1</td></tr>
<tr><td>table 2 row 2</td></tr>

In regards to the second table, dynamically add a className to the tr element for highlighting when using a simple tr or include a highlight prop if utilizing a custom component

<tr className={rowToHover === 1 ? 'hover-class' : ''} ><td>table 2 row 1</td></tr>

Assuming that your array will be rendered in a dynamic manner

Answer №2

apply a specific class to every row

<table>
  <tr class="table-row"><td>table 2 row 1</td></tr> // => highlighted
  <tr class="table-row"><td>table 2 row 2</td></tr>
  <tr class="table-row"><td>table 2 row 3</td></tr>
</table>

using scss file, (pseudo selector)

.table-row:hover {
  background-color: 'lightblue',
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React-Bootstrap's flexbox implementation for justify-content is malfunctioning

I am currently working on creating an Instagram clone using React, React-bootstrap, and SCSS. The Navbar has been created, but I am facing an issue where it is not centered with flexbox. How can I resolve this problem? Why is the flex center property no ...

Using the increment operator within a for loop in JavaScript

this code snippet causes an endless loop for (let i = 0; ++i;) { console.log(i) } the one that follows doesn't even run, why is that? for (let i = 0; i++;) { console.log(i) } I want a thorough understanding of this concept ...

Tips for using various versions of jQuery at the same time

Despite searching on Stack Overflow, none of the answers provided seem to work for my issue. My requirement is to utilize two separate versions of jQuery. Below is the code snippet: I first link to the initial version and use the following code: <sc ...

The issue with Jquery.Validate not functioning properly when trying to upload a file

I have integrated jQuery validation into my ASP.NET MVC project, and it is functioning correctly with textboxes. However, I am encountering an issue with file uploads. Below is the code snippet I am using: @model ffyazilim.Management.Model.Credential.Crea ...

Angular prevents the page from reloading when using `$window.location`

I'm facing an issue while trying to refresh my admin page using Angular. I've come across $window.location.reload() and $route.reload(). I attempted to use $window.location.reload() by injecting $window into the function parameters. scope.uploadL ...

Alert displayed at the center of the browser when the value surpasses the specified number

My webpage retrieves SNMP data using PHP and displays it with HTML, color-coding the values. I want to add a pop-up alert when a value exceeds a certain number. I have attempted different jQuery solutions without success. The PHP code to get the value: ...

The global CSS styles in Angular are not being applied to other components as expected

Currently utilizing Angular v10, I have a set of CSS styles that are meant to be used across the entire application. To achieve this, I added them to our global styles.css file. However, I'm encountering an issue where the CSS is not being applied to ...

Struggling to filter an Array within an Array based on dates falling between a specific date range

Currently, I am using a filtering method that is working perfectly, but I am facing an issue where I lose my original Array when there are no dates between the specified range (as expected). Is there a way to prevent this data loss? The reason I need to r ...

Using the textbox input to trigger an alert message

Just starting out with JavaScript and I'm attempting to create an alert box that not only displays the message "Thank You For Visiting" when you click the Enter button, but also includes the name entered into the text box. While I have successfully im ...

There have been no updates made to the SANITY Server while in Production Preview Mode

IMPLEMENTING 1 PROPERTY UPDATE IN A SCHEMATYPE Simply by including a single defineField: defineField({ name: 'surname', title: 'Surname', type: 'string', }), in an existing document named "author.ts", w ...

The value of `this` from React Store is currently returning null

I'm facing an issue where the this keyword is coming back as null in my store within a React component. I suspect that binding might be necessary, but I'm not entirely sure. Additionally, I am utilizing React Router and have the component wrapped ...

The async module has already been invoked with a callback function

Below is an array that I am working with: var files = [ { name: 'myfile.txt' }, { name: 'myfile2.txt' } ]; My goal is to access these objects asynchronously and send them for extraction, as shown below: Extraction function: ...

Utilizing React to Style Background Positions

I've been struggling to position a block of rendered jsx on the right side of the first block for hours now. Despite trying various options like marginTop, marginLeft, and even backgroundPosition based on my research, I still haven't been success ...

What steps should I take to repair the anchor tag associated with the badge?

Below is the code I have written for incorporating the badge feature: <Tab label={ <Badge badgeContent={size} color="secondary" showZero className={classes.badge} anchorOrigin={{ ...

Is it better to overwrite past values in CSS or to duplicate rules?

Which one of these CSS code practices is considered better? A - concise and rewritten: @media only screen and (min-width: 480px) and (max-width: 960px) { h1, h2, .header, .widget, .copyright { position: static; width: 100%; height: auto; ...

How can I modify certain attributes within an array of objects?

https://i.sstatic.net/TKkcV.jpgI need help with updating properties within an object array. Below is my code for reference. I have an object array consisting of two arrays, where the first one contains attribute "first" and the second one contains "last". ...

I'm encountering an issue in my node application where it is unable to access the push

I am a beginner in the world of node.js and express. Whenever I try to start my application using the command npm start, I encounter an error message saying Cannot Read property push of undefined from my index.js file. The problematic code snippet looks l ...

In Angular 2, you can include a routerLink in a child component that directs to the root

Currently, I am developing a web application using Angular 2 Beta 8 and encountering an issue with nested routes while utilizing the routerLink directive. The structure of the router is as follows: AppCmp |-> NewItemFormCmp |-> UserDashboardCmp ...

Is it necessary to uncheck the row checkbox when inputs are left empty after blurred?

Two issues have cropped up here. When clicking on an input within a row, the corresponding checkbox should be checked. Currently, only the first text input is able to check the checkbox due to the use of .prev(). Is there a different approach that can be t ...

insert a new DOM element into an array using jQuery

Looking at the code snippet below: a_ajouter = $('.question'); hidden_div.push(a_ajouter); console.log(hidden_div); Upon examining the output in the console, instead of a DOM object being added to the div as intended, it shows &apo ...