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

2

1 Answer 1

3

You just have to change this

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

To:

 this.animateRef.current.classList.add(`animation_trigger`);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for answering, do you know or do you have an idea about the rationale behind why classList worked and className failed?
Yes, String.concat does not change the string. It returns a new string concatenated with the previous one.
And make sure you delete } in ('animation_trigger}')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.