Animate the background of a table cell (td) in React whenever a prop is updated

I have a dynamic table that receives data from props. I would like the background animation of each cell (td) to change every time it receives new props. I've tried creating an animation for the background to indicate when a cell is updated, but it only shows the background color the first time a cell is created. Subsequent updates do not trigger the background animation.

import React from 'react'

import { TableRow, TableCell } from '@material-ui/core/'
import { makeStyles } from '@material-ui/core/styles'

const useStyles = makeStyles(theme => ({
  cell: {
    background: 'none',
    animation: `$bgColor 3s`,
    animationIterationCount: 1
  },
  '@keyframes bgColor': {
    '0%': {
      background: '#aacc00'
    },
    '100%': {
      background: 'none'
    }
  }
}))

const RenderBodyTable = ({ rows, isClient, filter, dataRow }) => {
  const classes = useStyles()

  return rows.map(row => (
    <TableRow key={row.id}>
      <TableCell className={classes.cell}>{row.name}</TableCell>
      <TableCell className={classes.cell}>{row.family}</TableCell>
      <TableCell className={classes.cell}>{row.tell}</TableCell>
      <TableCell className={classes.cell}>{row.address}</TableCell>
    </TableRow>
  ))
}

export default React.memo(RenderBodyTable)

Answer №1

To ensure your animation is triggered by a prop change, you will need to implement some logic to detect the change and update the styling accordingly.

As of now, your animation only activates when the page loads because this specific logic is missing.

There are multiple approaches you can take to address this issue, but it's recommended to refer to this helpful tutorial as a starting point.

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

Creating a standalone Wordpress child theme with its own CSS

After creating a child theme based on the responsive i-transform theme from http://templatesnext.org/itrans/, I found myself continuously undoing the i-transform CSS, which is taking up a lot of my time. However, if I remove the entire parent stylesheet, t ...

Maintaining hot reload functionality by sharing components between two projects

We are currently working on developing 2 products utilizing Angular 2 (although the same issue may arise with React). Our goal is to find a way to share components between these two products. Initially, we considered breaking things up into npm modules as ...

In React, when I attempt to pass color props to an SVG component, the color of the SVG does not update as

I've added a new SVG component called SvgAddnew with the following code: import * as React from 'react'; function SvgAddnew(props: React.SVGProps<SVGSVGElement>) { return ( <svg width="1em" height="1em" vi ...

Choose the immediate sibling located right after a specific element

My goal is to have the second paragraph following a h1 element display a dropcap, with the first being reserved for the author and date. Although I've tried using different CSS combinations like h1 + p::first-letter {}, it only affects the first para ...

The act of exporting components from the main index file allows for

I have assigned a components folder where I created various components that I want to export to the index.js file and then export all of them from there. This is an example from one of the components files: export default ToggleSwitch; Now, in the inde ...

JavaScript functions are not being triggered when the submit button is clicked

I am facing an issue where the form buttons are not triggering functions in my React application. The post request is being made using Axios. Could this be related to the structure of my components? Or perhaps it has something to do with file naming conven ...

hint.css guide for inserting line breaks into hints

Is there a way to manually insert line breaks in Hint.css hints? I've attempted using <br>, \n,, and various combinations of them. I've also experimented with the following CSS properties: white-space: normal; word-wrap: break-word; ...

Bootstrap dropdown malfunction

I am having trouble with my dropdown menu. The browser is cutting off the blue box in the dropdown area. I'm not sure if I need to add some CSS or make changes to the Bootstrap code to fix this issue. I haven't made any unusual modifications, jus ...

How to effectively manage multiple stylesheet links in React Native Expo development

Hello, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS/JS along with the basics of React. As a beginner developer, my current project involves creating a Study planner app for myself using React Native ...

Traverse div elements using jQuery and showcase the text within them

I've got a setup like the one below. I want to use jQuery to grab each link and display its href right below it. Instead of writing code for each div individually, I'm looking for a solution that works for all divs with the 'col' class, ...

Avoiding white spaces in user input with Material UI (MUI)

<FormControl variant="standard"> <InputLabel className="input-box-label" htmlFor="component-helper">My Value</InputLabel> <Input id="component-helper" value={m ...

Is it possible to incorporate a React app as a component within a static HTML page?

Looking to integrate the react-csv-viewer component into a static HTML page without deploying it on a local server. I've tried following the instructions from the React tutorial, but am struggling with the build process. My goal is simple - to use th ...

Solutions for resolving the error "Unable to access property 'value1' because it is undefined"

constructor() { super(); this.state = { value1 : Math.floor(Math.random() * 100), value2 : Math.floor(Math.random() * 100), value3 : Math.floor(Math.random() * 100), proposedAnswer : Math.floor(Math.random() * 3) + this.state.value1 + t ...

What is the best way to show only one div at a time when selecting from navbar buttons?

To only display the appropriate div when clicking a button on the left navbar and hide all others, you can use this code: For example: If "Profile" is clicked in the left navbar, the My Profile Form div will be displayed (and all others will remain hidde ...

Horizontalize your lists

Is there a way to make the li elements horizontal in this pagination setup? I've attempted display: inline and display: float, but neither seem to be working for me. <ul class="pagination" style="display:inline; text-align: center; margin:0;">& ...

To pass an interface property as an argument to another functional component

How do I reference an interface property as a parameter value in another functional component? interface InterfaceProperties { interfaceProperty1Id: number, interfaceProperty1Name : string } const tabsInterfaces: Map<InterfaceDetailEnum, JSX.Elemen ...

Prevent the layout from shifting with CSS

I am faced with a challenge regarding a grid of images that are dynamically generated on the screen. The number of images in each row of the grid is dependent on the size of the page. While everything functions properly when the page is at a certain size, ...

Managing a project with multiple pages using create-react-app

Lately, I've been experimenting with create-react-app and noticed that it only supports one default page called 'index.html'. If my project has multiple pages, can I still use create-react-app for front-end development? How can I achieve thi ...

Use Flexbox hover effect to display submenu text exclusively in the column being hovered

Having an issue with a 5 column flexbox. Each box should display a header text and supplementary text only when hovered over, returning to normal when unhovered. I was able to accomplish this in Codepen (view the rough model here). However, when trying to ...

Launching the webpage with Next.js version 13

Hey there! I'm currently working on implementing a loading screen for my website to enhance the user experience. Since it's quite a large site, I believe a loading screen would be beneficial. However, I'm having trouble getting it to work on ...