Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing transition effects in React JS when state changes

I have an image on a React page. When the state is updated to a new image I want to perform the following transition effect:

  • The original image should zoom in and fade out
  • The new image should also zoom in and fade in

The effect should look similar to passing through a wall to a new scene.

How am I able to do this in React?

like image 882
kojow7 Avatar asked Sep 25 '18 21:09

kojow7


People also ask

How do you animate when React state changes?

You can use CSS classes to animate in React import React, { useState } from 'react'; import classNames from 'classnames'; import styles from './App. module. css'; const App = () => { const [animate, setAnimate] = useState(false); const handleClick = () => setAnimate(!

What happens when state is updated in React?

This is a function available to all React components that use state, and allows us to let React know that the component state has changed. This way the component knows it should re-render, because its state has changed and its UI will most likely also change.


3 Answers

As @pgsandstrom mentioned, React Transition Group is the way to go. Unfortunately, it's not very developer friendly (pretty steep learning curve).

Here's a working example: https://codesandbox.io/s/6lmv669kz

✔ Original image zooms in while fading out

✔ New image zooms in while fading in

TransitionExample.js

import random from "lodash/random";
import React, { Component } from "react";
import { CSSTransition, TransitionGroup } from "react-transition-group";
import uuid from "uuid/v1";

const arr = [
  {
    id: uuid(),
    url: `https://loremflickr.com/600/100?lock=${random(0, 999)}`
  },
  {
    id: uuid(),
    url: `https://loremflickr.com/600/100?lock=${random(0, 999)}`
  },
  {
    id: uuid(),
    url: `https://loremflickr.com/600/100?lock=${random(0, 999)}`
  }
];

export default class TransitionExample extends Component {
  state = {
    index: 0,
    selected: arr[0]
  };

  nextImage = () =>
    this.setState(prevState => {
      const newIndex = prevState.index < arr.length - 1 ? prevState.index + 1 : 0;
      return {
        index: newIndex,
        selected: arr[newIndex]
      };
    });

  render = () => (
    <div className="app">
      <div style={{ marginBottom: 30, height: 100 }}>
        <TransitionGroup>
          <CSSTransition
            key={this.state.selected.id}
            timeout={1000}
            classNames="messageout"
          >
            <div style={{ marginTop: 20 }}>
              <img className="centered-image" src={this.state.selected.url} />
            </div>
          </CSSTransition>
        </TransitionGroup>
      </div>
      <div style={{ textAlign: "center" }}>
        <button
          className="uk-button uk-button-primary"
          onClick={this.nextImage}
        >
          Next Image
        </button>
      </div>
    </div>
  );
}

styles.css

.app {
  margin: 0 auto;
  overflow: hidden;
  width: 700px;
  height: 800px;
}

.centered-image {
  display: block;
  margin: 0 auto;
}

/* starting ENTER animation */
.messageout-enter {
  position: absolute;
  top: 0;
  left: calc(13% + 5px);
  right: calc(13% + 5px);
  opacity: 0.01;
  transform: translateY(0%) scale(0.01);
}

/* ending ENTER animation */
.messageout-enter-active {
  opacity: 1;
  transform: translateY(0%) scale(1);
  transition: all 1000ms ease-in-out;
}

/* starting EXIT animation */
.messageout-exit {
  opacity: 1;
  transform: scale(1.01);
}

/* ending EXIT animation */
.messageout-exit-active {
  opacity: 0;
  transform: scale(4);
  transition: all 1000ms ease-in-out;
}
like image 54
Matt Carlotta Avatar answered Oct 22 '22 19:10

Matt Carlotta


It sounds like you are looking for React Transition Group. It is the "official" way of solving these issues. Specifically I think this is what you should use. It can be a bit tricky to get a hang of, but it is really nice and powerful once you understand it.

like image 6
pgsandstrom Avatar answered Oct 22 '22 19:10

pgsandstrom


This worked for me (link):

index.js:

import React from "react";
import { render } from "react-dom";

import "./styles.scss";

const src1 =
  "https://www.nba.com/dam/assets/121028030322-james-harden-traded-102712-home-t1.jpg";
const src2 = "https://www.nba.com/rockets/sites/rockets/files/wcwebsite.jpg";

var state = {
  toggle: true
};

class App extends React.Component {
  render() {
    const cn1 = "imgFrame " + (state.toggle ? "toggleOut" : "toggleIn");
    const cn2 = "imgFrame " + (state.toggle ? "toggleIn" : "toggleOut");
    return (
      <div>
        <img className={cn1} src={src1} alt={"img1"} />
        <img className={cn2} src={src2} alt={"img2"} />
        <button
          onClick={() => {
            state.toggle = !state.toggle;
            this.forceUpdate();
          }}
        >
          click me to toggle
        </button>
        <h1>Hello</h1>
      </div>
    );
  }
}

render(<App />, document.getElementById("app"));

style.scss:

html,
body {
  background-color: papayawhip;
  font-family: sans-serif;

  h1 {
    color: tomato;
  }
}

@keyframes fadeout {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(0.9);
  }
}

@keyframes fadein {
  0% {
    opacity: 0;
    transform: scale(1.1);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

.toggleOut {
  animation: fadeout 500ms;
  opacity: 0;
}

.toggleIn {
  animation: fadein 500ms;
  opacity: 1;
}

.imgFrame {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 200px;
  height: 200px;
}

button {
  position: absolute;
  top: 220px;
}
like image 6
Kevin He Avatar answered Oct 22 '22 19:10

Kevin He