Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
How do I trigger CSS animation on react element that has changed due to state change?

I'm trying to recreate this effect: https://imgur.com/a/4iu2ScT Currently I'm rendering an with the "src" based on the state. Like this

import React from "react";
import styled from "styled-components";
import home_food from "../images/home/home_aji_de_gallina.jpg"
import food from "../images/menu/entry/causa.jpeg"

import { useState } from "react";

const TestBoks = styled.div`
    background-color:red;
    width: 30px;
    height: 30px;
    opacity: ${props => props.opacity || 0.6};
    transition: ease-in-out;
`;
const TestContainer = styled.div`

    display: flex;
    width: 100%;
    justify-content: space-around;
`;
const WholePage = styled.div`   

    height: 100vh;
    width: 100%;

    img{
        width: 100px;
        transition: ease-in;
    }
`

const pictures = [home_food,food]

const Test = () => {

    const [Picture, setPicture] = useState(pictures[0])

    function setNewPicture(index_number){
        setPicture(pictures[index_number])
        
    }

    return (

        <WholePage> 

        <TestContainer>
            <TestBoks key={Picture[0]} onMouseEnter={() => setNewPicture(0)}/>
            <TestBoks key={Picture[1]} onMouseEnter={() => setNewPicture(1)}/>
        </TestContainer>  

        <img src={Picture} alt="ononon" />
           
        </WholePage>
    );
};

export default Test;

And it ends up looking like this: https://imgur.com/a/bTJLTAC

However, it ignores the animation I described in the styled.div

So my question is, how do i trigger an css animation on a render caused by a state change?

If there is a better way to achieve the effect linked in the Imgur, please share that as well

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should use useEffect in order to listen for state changes. see: https://reactjs.org/docs/hooks-effect.html

Also, don't forget to import: import { useEffect } from 'react';

const Test = () => {

  const [Picture, setPicture] = useState(pictures[0])
  const [newPicture, setNewPicture] = useState(0)

  useEffect(() => {
    setPicture(pictures[newPicture])
  }, [newPicture]);

  return (

      <WholePage> 

      <TestContainer>
          <TestBoks key={Picture[0]} onMouseEnter={() => setNewPicture(0)}/>
          <TestBoks key={Picture[1]} onMouseEnter={() => setNewPicture(1)}/>
      </TestContainer>  

      <img src={Picture} alt="ononon" />
         
      </WholePage>
  );
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda