6

here my demo: https://stackblitz.com/edit/react-pwdkse note: use your browser console instead of the Stackblitz's one. Seems the browser console is much more complete in terms of information_feedback

I would trigger animation using ReactJS ref's references instead of changing className inside the element's scope. Currently nothing is happening.

What I may missing?

here my Reacts' snippets:

component

import React, { Component } from 'react';
import { render } from 'react-dom'; 
import './style.module.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
     this.animateRef = React.createRef();
  //  this.triggerAnimation = this.triggerAnimation.bind(this);
  }

  componentDidMount(){ 
    // empty scope right now
  }

   triggerAnimation=()=>{ 
      console.log("trigger animateRef animation")

      //   this.animateRef.current.style.animationName="animateElement"
      //  this.animateRef.current.style.animation="1.5s 4.3s 3 alternate forwards"
       this.animateRef.current.className.concat(`animation_trigger`)
        console.log("animateRef: ", this.animateRef)
  }



  render() {

    return (
      <div>

        <p>
          Start editing to see some magic happen :)
        </p>

        <div className="animatedElementStyle" ref={this.animateRef}>
            I am rendered !
        </div>
        <button onClick={this.triggerAnimation}>
          trigger animation
        </button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

stylesheet

h1, p {
  font-family: Arial;
}

.animatedElementStyle{ 
    position:absolute;
    top:61%;
    left:10%;
    width:15w;
    height:25vh;
    visibility: hidden; 
    opacity:0;    
}

.animation_trigger{
    animation: animateElement 1.5s 0.5s 3 alternate forwards;
}

@keyframes animateElement{
    from{  
        opacity:0;
        visibility:hidden; 
    }
    100%{
        transform: translate(15%,0);
        opacity:1;
        visibility:visible;
        color:orange;
        font-size:3em;
    }
}

thanks for any hint

Webwoman
  • 10,196
  • 12
  • 43
  • 87
  • related question: https://stackoverflow.com/questions/36403101/toggle-class-in-react – Webwoman Nov 10 '19 at 14:57
  • CSS animation only happens when the DOM element is created; did you mean [CSS transition](https://developer.mozilla.org/en-US/docs/Web/CSS/transition)? see also https://stackoverflow.com/questions/20586143/css-animation-vs-transition – Aprillion Nov 10 '19 at 15:04

1 Answers1

3

You just have to change this

this.animateRef.current.className.concat(`animation_trigger`)

To:

 this.animateRef.current.classList.add(`animation_trigger`);
  • thanks for answering, do you know or do you have an idea about the rationale behind why classList worked and className failed? – Webwoman Nov 10 '19 at 15:18
  • 1
    Yes, [String.concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) does not change the string. It returns a new string concatenated with the previous one. – Payam Mohammadi Nov 10 '19 at 15:21
  • And make sure you delete `}` in `('animation_trigger}')` – Payam Mohammadi Nov 10 '19 at 15:23